如何通过识别键并对值进行求和来组合多维PHP数组键

时间:2014-10-01 14:14:30

标签: php arrays sum

下面是一个由子数组组成的数组。每个子节点都是数据库中的结果集 对于特定事件,它们使用[event_id]字段按其分组。我想要 总结客户的积分,然后一旦完成,遍历数组并添加一个新元素 为每个子数组调用[point_total]。匹配基于[customer_id]的数组会将它们的点总计,然后添加到包含该客户ID的每个数组中。我不确定是否可以一次完成所有操作,或者如果我必须在累计点数后第二次遍历数组以便为客户添加total_points字段。

结构:

[0] => Array
    (
        [0] => Array
            (
                [customer_id] => 962
                [event_id] => 1
                [score] => 356
                [point] => 1
            )

        [1] => Array
            (
                [customer_id] => 962
                [event_id] => 1
                [score] => 356
                [point] => 1
            )
    )
[1] => Array
    (
        [0] => Array
            (
                [customer_id] => 962
                [event_id] => 2
                [score] => 356
                [point] => 1
            )

        [1] => Array
            (
                [customer_id] => 962
                [event_id] => 2
                [score] => 356
                [point] => 1
            )
    }
[2] => Array
    (
        [0] => Array
            (
                [customer_id] => 962
                [event_id] => 3
                [score] => 356
                [point] => 1
            )

        [1] => Array
            (
                [customer_id] => 962
                [event_id] => 3
                [score] => 356
                [point] => 1
            )
    }

2 个答案:

答案 0 :(得分:0)

您需要一些嵌套的foreach循环和一个临时数组:

$temp=array();
foreach($data as $element)
    foreach($element as $child)
        $temp[$child['customer_id']] = isset($temp[$child['customer_id']])?$temp[$child['customer_id']]+$child['score']:$child['score'];

foreach($data as &$element)
    foreach($element as &$child)
        $child['total']=$temp[$child['customer_id']];

实例:http://codepad.viper-7.com/HkNF67

注意这取决于您想要总分数,如果您想要总分而不是$child['score']代替$child['point']

答案 1 :(得分:0)

您需要两个步骤:首先遍历数组并计算每customer_id个总点数。然后再次遍历它并将total_point附加到每个客户:

// Step 1

function reduce_points_total($carry, $item) {
    if (is_array($item)) {
        if (isset($item['customer_id']) && isset($item['point'])) {
            if (!isset($carry[$item['customer_id']])) $carry[$item['customer_id']] = 0;
            $carry[$item['customer_id']] += $item['point'];
        } else {
            $carry = array_reduce($item, reduce_points_total, $carry);
        }
    }
    return $carry;
}

$points_total = array_reduce($array, reduce_points_total, array());

// Step 2

function insert_points_total(&$item, $key, $points_total) {
    if (is_array($item)) {
        if (isset($item['customer_id']) && isset($points_total[$item['customer_id']])) {
            $item['points_total'] = $points_total[$item['customer_id']];
        } else {
            array_walk($item, insert_points_total, $points_total);
        }
    }
}

array_walk($array, insert_points_total, $points_total);

步骤1中的递归array_reduce计算$points_totale。它生成一个数组:

Array
(
    [962] => 4
    [963] => 3
)

步骤2中的递归array_walk()会将这些数字插入主$array

在这里您可以找到正在运行的演示:https://eval.in/200799