如何将二维数组转换为集合laravel?
问题描述:
我有这样的数组:
$test = array(
array(
'name' => 'Christina',
'age' => '25'
),
array(
'name' => 'Agis',
'age' => '22'
),
array(
'name' => 'Agnes',
'age' => '30'
)
);
我想将其更改为laravel集合
I want to change it to collection laravel
我这样尝试:
collect($test)
结果并不完美.仍然有一个数组
The results are not perfect. There is still an array
我该如何解决这个问题?
How can I solve this problem?
答
collect($test)
不会将$test
转换为集合,而是将$test
作为集合返回.您需要将其返回值用于新变量,或覆盖现有变量.
collect($test)
does not convert $test
to a collection, it returns $test
as a collection. You need to use it's return value for a new variable, or override the existing one.
$test = collect($test);
如果您要像下面的注释中所示将单个项目转换为对象(而不是数组),则需要转换它们.
If you want to convert the individual items to objects (instead of arrays) like you indicated in the comment below, then you will need to cast them.
$test = collect($test)->map(function ($item) {
return (object) $item;
});