As shown below I have a multi-dimensional array with an associative array in the third level that I am trying to manipulate based on gamesCount
. I want to generate a new array of items with the highest gamesCount
in each sport. Is there an efficient way to this without going overkill?
EDIT: It could be n
number of sports hence the goal is to find the max number for gameCounts
for each sport.
Initial array:
$array = [
[
["sport" => "soccer", "gamesCount" => 5, "gamesId" => 1],
["sport" => "soccer", "gamesCount" => 3, "gamesId" => 2],
["sport" => "soccer", "gamesCount" => 10, "gamesId" => 3],
["sport" => "soccer", "gamesCount" => 10, "gamesId" => 4],
],
[
["sport" => "basketball", "gamesCount" => 1, "gamesId" => 5],
["sport" => "basketball", "gamesCount" => 3, "gamesId" => 6],
["sport" => "basketball", "gamesCount" => 3, "gamesId" => 7],
["sport" => "basketball", "gamesCount" => 8, "gamesId" => 8],
]
];
Desired Result:
array(3) {
[0]=>
array(3) {
["sport"]=>
string(6) "soccer"
["gamesCount"]=>
int(10)
["gamesId"]=>
int(3)
}
[1]=>
array(3) {
["sport"]=>
string(6) "soccer"
["gamesCount"]=>
int(10)
["gamesId"]=>
int(4)
}
[2]=>
array(3) {
["sport"]=>
string(10) "basketball"
["gamesCount"]=>
int(5)
["gamesId"]=>
int(8)
}
}