How can I convert array two dimensional to collection laravel?
Asked Answered
M

3

7

I have array like this :

$test = array(
    array(
        'name' => 'Christina',  
        'age' => '25' 
    ),
    array(
        'name' => 'Agis', 
        'age' => '22'
    ),
    array(
        'name' => 'Agnes', 
        'age' => '30'
    )
);

I want to change it to collection laravel

I try like this :

collect($test)

The results are not perfect. There is still an array

How can I solve this problem?

Martine answered 23/7, 2018 at 23:50 Comment(1)
Is there no one who can help?Martine
C
18

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;
});
Cavern answered 24/7, 2018 at 2:53 Comment(3)
If I dd{$test) the result like this : postimg.cc/image/ro3mko4d9. It is an array. I want the result like this : postimg.cc/image/fyzmwq0jx. So I want it to be an object. Not an arrayMartine
You're talking about the individual array items? If you want them to be objects then you would need to cast them to objects. I've updated the answer to show how you might do this.Cavern
Maybe you can help me again. Look at this : #51488269. I want to change it using chunk. But i'm still confused to implement itMartine
D
5

I know it's been a while, but i found this answer on laracast and it seems to solve the issue better, cause it make it recursive. This solution I've got from https://gist.github.com/brunogaspar/154fb2f99a7f83003ef35fd4b5655935 github and works like a charm.

\Illuminate\Support\Collection::macro('recursive', function () {
return $this->map(function ($value) {
    if (is_array($value) || is_object($value)) {
        return collect($value)->recursive();
    }

    return $value;
});

});

than you go like:

$data = [
[
    'name' => 'John Doe',
    'emails' => [
        '[email protected]',
        '[email protected]',
    ],
    'contacts' => [
        [
            'name' => 'Richard Tea',
            'emails' => [
                '[email protected]',
            ],
        ],
        [
            'name' => 'Fergus Douchebag', // Ya, this was randomly generated for me :)
            'emails' => [
                '[email protected]',
            ],
        ],
    ],
  ],
];
$collection = collect($data)->recursive();
Diann answered 16/4, 2021 at 12:1 Comment(0)
C
2

To share more light.

Collections are "macroable", which allows you to add additional methods to the Collection class at run time. According to Laravel explanation on collections. Arrays can be dimensional. using the map function extends your collection to convert child array into objects

$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.


Charged answered 20/2, 2019 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.