<?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);
- Output for 7.1.0 - 7.1.33, 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:
72.95 ms | 409 KiB | 5 Q