php 按二级键对2d数组数据分组,并计算各组内的平均值

whlutmcx  于 2023-01-04  发布在  PHP
关注(0)|答案(3)|浏览(134)

我有一个包含名称和整数值的二维数组。行中并不总是包含所有名称。我需要按名称对数据进行分组,并求出每组中所有值的平均值。
样品阵列:

$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
    ]
];

预期结果:

[
  'Armstrong_Tony' => 13,
  'Blair_Jarryd' => 15.333333333333334,
  'Broomhead_Tim' => 17,
  'Ball_Luke' => 20,
  'Beams_Dayne' => 14,
  'Frost_Jack' => 8,
  'Goldsack_Tyson' => 7,
]

我的编码尝试:

$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);
}
jk9hmnmh

jk9hmnmh1#

这应该适用于您获取所有关键点和所有平均值:
在这里,我遍历每个innerArray并获取所有键值,然后将它们保存到一个数组中,我对该数组进行了扁平化,只获取唯一值
然后我使用array_column()(对于PHP +5.5),count()array_sum()来计算平均值。

<?php

    $arr = array(
                array(
                    "Armstrong_Tony" => 7,
                    "Blair_Jarryd" => 7,
                    "Broomhead_Tim" => 11
                ),
                array(
                    "Armstrong_Tony" => 19,
                    "Ball_Luke" => 20,
                    "Blair_Jarryd" => 20,
                    "Broomhead_Tim" => 23
                ),
                array(
                    "Ball_Luke" => 20,
                    "Beams_Dayne" => 14,
                    "Blair_Jarryd" => 19,
                    "Frost_Jack" => 8,
                    "Goldsack_Tyson" => 7
                )
        );

    $keys = array();
    $average = array();

    //Get Keys
    foreach($arr as $innerArray)
        $keys[] = array_keys($innerArray);

    $keys = array_unique(call_user_func_array('array_merge', $keys));

    //Get average
    foreach($keys as $key) {
        $values = array_column($arr, $key);
        $average[$key] = array_sum($values) / count($values);
    }

    print_r($average);

?>

输出:

Array
(
    [Armstrong_Tony] => 13
    [Blair_Jarryd] => 15.333333333333
    [Broomhead_Tim] => 17
    [Ball_Luke] => 20
    [Beams_Dayne] => 14
    [Frost_Jack] => 8
    [Goldsack_Tyson] => 7
)
qnzebej0

qnzebej02#

你需要创建一个包含每个键的总和和计数(键在所有数组中出现的次数)的数组。一旦你编译了这些信息,循环数组,用总和除以计数得到平均值。

$myArrays = array(
    array('Armstrong_Tony' => 2),
    array('Ball_Luke' => 3),
);

$sums = array();

foreach($myArrays as $array) {
    foreach($array as $key => $value) {
        if(!isset($sums[$key]))
            $sums[$key] = array('count' => 0, 'sum' => 0);

        $sums[$key]['count']++;
        $sums[$key]['sum'] += $value;
    }
}

$results = array();

foreach($sums as $key => $sum) {
    $results[$key] = $sum['sum']/$sum['count'];
}

print_r($results);
zsbz8rwp

zsbz8rwp3#

作为一种现代的优雅方法,首先使用array_merge_recursive()和spread操作符按关联键对数据进行分组,然后在array_map()中将分组数据转换为数组(因为单独的值不会形成子数组),然后进行array_sum()count()的标准调用。
代码:(Demo

var_export(
    array_map(
        function($data) {
            $data = (array) $data;
            return array_sum($data) / count($data);
        },
        array_merge_recursive(...$array)
    )
);

或者使用PHP7.4的箭头函数:

var_export(
    array_map(
        fn($data) => array_sum((array) $data) / count((array) $data),
        array_merge_recursive(...$array)
    )
);

相关问题