如何将二维数组转换为集合 Laravel? [英] How can I convert array two dimensional to collection laravel?
本文介绍了如何将二维数组转换为集合 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;
});
这篇关于如何将二维数组转换为集合 Laravel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文