I have a two-dimensional array containing names and integer values. The rows do not consistently contain all names. I need to group the data by name and find the average of all values in each group.
Sample array:
$games = [
[
"Armstrong_Tony" => 7,
"Blair_Jarryd" => 7,
"Broomhead_Tim" => 11
],
[
"Armstrong_Tony" => 19,
"Ball_Luke" => 20,
"Blair_Jarryd" => 20,
"Broomhead_Tim" => 23
],
[
"Ball_Luke" => 20,
"Beams_Dayne" => 14,
"Blair_Jarryd" => 19,
"Frost_Jack" => 8,
"Goldsack_Tyson" => 7
]
];
Desired result:
[
'Armstrong_Tony' => 13,
'Blair_Jarryd' => 15.333333333333334,
'Broomhead_Tim' => 17,
'Ball_Luke' => 20,
'Beams_Dayne' => 14,
'Frost_Jack' => 8,
'Goldsack_Tyson' => 7,
]
My coding attempt:
$arr = array_merge_recursive($games[0],$games[1],$games[2],$games[3],$games[4]);
ksort($arr);
foreach($arr as $player => $val) {
if(array_key_exists($player, $games[0])) {
$prev_game_0 = $games[0][$player];
} else {
$prev_game_0 = "";
}
if(array_key_exists($player, $games[1])) {
$prev_game_1 = $games[1][$player];
} else {
$prev_game_1 = "";
}
if(array_key_exists($player, $games[2])) {
$prev_game_2 = $games[2][$player];
} else {
$prev_game_2 = "";
}
if(array_key_exists($player, $games[3])) {
$prev_game_3 = $games[3][$player];
} else {
$prev_game_3 = "";
}
if(array_key_exists($player, $games[4])) {
$prev_game_4 = $games[4][$player];
} else {
$prev_game_4 = "";
}
$last_5_array[$player] = array($prev_game_0, $prev_game_1, $prev_game_2, $prev_game_3, $prev_game_4);
}