PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $array = collect([ ['name' => 'Judi', 'age' => 15, 'hobby' => 'playing a game'], ['name' => 'Judi', 'age' => 15, 'hobby' => 'swimming'], ['name' => 'Judi', 'age' => 15, 'hobby' => 'playing a basketball'], ['name' => 'Jedi', 'age' => 14, 'hobby' => 'coding'], ['name' => 'Jedi', 'age' => 14, 'hobby' => 'reading'], ['name' => 'Jedi', 'age' => 14, 'hobby' => 'listen to the music'], ]); var_export( $array ->groupBy('name') ->map(fn($group) => collect($group->first())->put('hobby', $group->pluck('hobby')->implode(', '))) ->values() ->toArray() ); echo "\n---\n"; var_export( collect($array)->groupBy('name')->map(function ($group) { return ['hobby' => $group->pluck('hobby')->join(', ')] + $group->first(); })->values()->toArray() );
Show:  
Copy Clear