下面是laravel的eloquent从数据库返回的当前数组。我想将一些东西插入数组,我可以知道怎么做吗?
返回的数据
Company Object (
[attributes] => Array (
[id] => 1
[company_name] => superman
[company_fullname] => Superman Ent. )
[original] => Array (
[id] => 1
[company_name] => superman
[company_fullname] => Superman Ent. )
[relationships] => Array ( )
[exists] => 1
[includes] => Array ( ) )
虽然我可以通过 foreach 数组调用它并通过 {{ $x->company_name }} 访问。我想用一些自定义信息扩展数组,例如成员总数?
我这样试过,失败了。
$temp = array("count" => "1232");
array_push($companyInfo, $temp);
我明白了
array_push() expects parameter 1 to be array, object given
更新 companyInfo 数组由 laravel 返回,由于我的愚蠢和粗心(几天不眠之夜 +_+),我没有注意到 ['attributes'] 里面的一切!从答案中应用方法后,可以通过以下方式访问数据。
{{ $x['attributes']['company_name'] }}
{{ $x[0]['count'] }}
最佳答案
$CompanyInfo 在你的例子中是一个对象。您需要指定一个命名参数来保存您的 $temp 数组信息:
$temp = array("count" => "1232");
$companyInfo->temp = $temp;
或者将对象转换成数组:
$temp = array("count" => "1232");
$companyInfo = (array) $companyInfo;
array_push($companyInfo, $temp);
关于php - 数组插入另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16643480/