3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [[ "continent" => "Europa", "country" => "France", "capital" => "Paris", ],[ "continent" => "Europa", "country" => "Spain", "capital" => "Madrid", ],[ "continent" => "Asia", "country" => "Russia", "capital" => "Moscow", ]]; $groupByProperty = function(array $list, string $property) { return array_reduce($list, function($grouped, $item) use(&$property) { $grouped[$item[$property]][] = $item; return $grouped; }, []); }; $groupedByContinent = $groupByProperty($data, 'continent'); var_dump($groupedByContinent);

preferences:
24.5 ms | 405 KiB | 5 Q