如何将二维数组转换为集合laravel? [英] How can I convert array two dimensional to collection laravel?

查看:547
本文介绍了如何将二维数组转换为集合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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆