我有这样的数组:
$test = array(
array(
'name' => 'Christina',
'age' => '25'
),
array(
'name' => 'Agis',
'age' => '22'
),
array(
'name' => 'Agnes',
'age' => '30'
)
);
我想把它改成收藏laravel
我试着这样:
collect($test)
结果并不完美。还有一个阵列
我怎么解决这个问题?
collect($test)
没有将$test
转换为集合,它将$test
作为集合返回。您需要将它的返回值用于新变量,或覆盖现有变量。
$test = collect($test);
如果您想将各个项目转换为对象(而不是数组),就像您在下面的注释中指出的那样,那么您将需要转换它们。
$test = collect($test)->map(function ($item) {
return (object) $item;
});
分享更多光。
集合是“可宏”的,它允许您在运行时向Collection类添加其他方法。根据Laravel对收藏品的解释。数组可以是维度的。使用map函数扩展您的集合以将子数组转换为对象
$test = array(
array(
'name' => 'Christina',
'age' => '25'
),
array(
'name' => 'Agis',
'age' => '22'
),
array(
'name' => 'Agnes',
'age' => '30'
)
);
// can be converted using collection + map function
$test = collect($test)->map(function($inner_child){
return (Object) $inner_child;
});
This will cast the inner child array into Object.