我想在 laravel 5.3 中合并两个数组。我有变量 '$type' 返回
`Illuminate\Support\Collection Object ( [items:protected] => Array ( [1] => rinu )`
这是从查询中得到的
$type0=DB::table('users')->where('name','=',$head)->pluck('name','id');
我想与返回的数组 $type1 合并
Illuminate\Support\Collection Object ( [items:protected] => Array ( [2] => john ) )
Illuminate\Support\Collection Object ( [items:protected] => Array ( [3] => dinesh ) )
Illuminate\Support\Collection Object ( [items:protected] => Array ( [4] => mahesh ) )
Illuminate\Support\Collection Object ( [items:protected] => Array ( ) )
$type1=DB::table('users')->where('name','=',$head)->pluck('name','id');
我尝试合并并将其存储在 $type0;
$type0=$type0->merge($type1);
它返回错误的值
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john ) )
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john [2] => dinesh ) )
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john [2] => dinesh [3] => mahesh ) )
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => rinu [1] => john [2] => dinesh [3] => mahesh ) )`
enter code here
如果你想收到合并的array
(并不是collection
), 您可以使用toArray() https://laravel.com/docs/5.3/collections#method-toarray and array_merge() http://php.net/manual/en/function.array-merge.php功能:
$mergedArray = array_merge($type0->toArray(), $type1->toArray());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)