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


How to combine multidimensional PHP array keys by identifying key and sum the values

下面是一个由子数组组成的单个数组。每个子项都是数据库上的结果集对于特定事件,并且使用CCD_ 1字段将它们分组。我想为客户求积分,然后完成后,遍历数组并添加一个新元素称为CCD_ 2。基于[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
            )
    }

您将需要几个嵌套的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']

您需要两个步骤:首先遍历数组并计算每个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中的递归CCD_ 8计算CCD_。它生成一个数组:

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

步骤2中的递归CCD_ 10将这些数字插入到主CCD_。

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