且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将二维数组转换为集合 Laravel?

更新时间:2023-09-02 17:50:22

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;
});