<?php
$data = [[
"continent" => "Europa",
"country" => "France",
"capital" => "Paris",
],[
"continent" => "Europa",
"country" => "Spain",
"capital" => "Madrid",
],[
"continent" => "Asia",
"country" => "Russia",
"capital" => "Moscow",
]];
$groupByContinent = function(array $list) {
return array_reduce($list, function($grouped, $item) {
$grouped[$item['continent']][] = $item;
return $grouped;
}, []);
};
$groupedByContinent = $groupByContinent($data);
var_dump($groupedByContinent);
- Output for 7.1.25 - 7.1.28, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
- array(2) {
["Europa"]=>
array(2) {
[0]=>
array(3) {
["continent"]=>
string(6) "Europa"
["country"]=>
string(6) "France"
["capital"]=>
string(5) "Paris"
}
[1]=>
array(3) {
["continent"]=>
string(6) "Europa"
["country"]=>
string(5) "Spain"
["capital"]=>
string(6) "Madrid"
}
}
["Asia"]=>
array(1) {
[0]=>
array(3) {
["continent"]=>
string(4) "Asia"
["country"]=>
string(6) "Russia"
["capital"]=>
string(6) "Moscow"
}
}
}
preferences:
83.81 ms | 409 KiB | 5 Q