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?

moses toh
  • 12,344
  • 71
  • 243
  • 443

3 Answers3

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;
});
Dwight
  • 12,120
  • 6
  • 51
  • 64
  • If I `dd{$test)` the result like this : https://postimg.cc/image/ro3mko4d9/. It is an array. I want the result like this : https://postimg.cc/image/fyzmwq0jx/. So I want it to be an object. Not an array – moses toh Jul 24 '18 at 03:08
  • 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. – Dwight Jul 24 '18 at 03:45
  • Maybe you can help me again. Look at this : https://stackoverflow.com/questions/51487769/how-to-insert-big-data-on-the-laravel. I want to change it using `chunk`. But i'm still confused to implement it – moses toh Jul 24 '18 at 04:06
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' => [
        'john@doe.com',
        'john.doe@example.com',
    ],
    'contacts' => [
        [
            'name' => 'Richard Tea',
            'emails' => [
                'richard.tea@example.com',
            ],
        ],
        [
            'name' => 'Fergus Douchebag', // Ya, this was randomly generated for me :)
            'emails' => [
                'fergus@douchebag.com',
            ],
        ],
    ],
  ],
];
$collection = collect($data)->recursive();
Rodrigo Klim
  • 96
  • 1
  • 3
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.


Codedreamer
  • 1,552
  • 15
  • 13