mardi 18 décembre 2018

Laravel Collections :multidimensional array, Group by keys and unique values

How would one do the following with laravel collections

$input =[
            ['name' =>'John McClane','age'=>'40' ],
            ['name' =>'John McClane','age'=>'50' ],
            ['name' =>'Hans Gruber','age'=>'40' ],
            ['name' =>'Hans Gruber','age'=>'50' ],
            ['name' =>'Holly McClane','age'=>'20' ],
            ['name' =>'Holly McClane','age'=>'49' ],
        ];


$output =[
            'name' => [
                'John McClane',
                'Hans Gruber',
                'Holly McClane'
            ],
            'age' =>[
                '40',
                '50',
                '20',
                '49'
            ]
        ];

I know how to do this in plain PHP , but I was wondering what a neat way would be using Collections.

Group by multidimensional keys, with unique values.

Cheers!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire