在PHP中,合并重复集的多维数组的元素,总结特定键的值 [英] In PHP, merge duplicate set of elements of an multidimensional array and sum the values of specific key

查看:133
本文介绍了在PHP中,合并重复集的多维数组的元素,总结特定键的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组,其中我试图合并有重量值重复的元素和总结键的值。

I have following array where I am trying to merge the elements which has shelf and weight value as duplicate and sum the value of piece key.

Array
(
    [0] => Array
        (
            [shelf] => Left
            [weight] => 10.000
            [piece] => 1
        )

    [1] => Array
        (
            [shelf] => Right
            [weight] => 04.000
            [piece] => 12
        )

    [2] => Array
        (
            [shelf] => Right
            [weight] => 04.000
            [piece] => 4
        )

    [3] => Array
        (
            [shelf] => Right
            [weight] => 07.000
            [piece] => 8
        )
)

目前我正在下面通过以下字段创建临时表所需的输出与下面的SQL语句的帮助下架重量和插入所有四个值与以下选择查询分析的结果在PHP

Currently I am getting following desired output with help of following SQL statement by creating the temporary table with following fields shelf, weight and piece and inserting all the four values and parsing the result in PHP with following select query

SELECT货架,重量,SUM(件)FROM温度GROUP BY CONCAT(货架,重量)

Array
(
    [0] => Array
        (
            [shelf] => Left
            [weight] => 10.000
            [piece] => 1
        )

    [1] => Array
        (
            [shelf] => Right
            [weight] => 04.000
            [piece] => 16
        )

    [3] => Array
        (
            [shelf] => Right
            [weight] => 07.000
            [piece] => 8
        )
 )

不过,我相信,这可以通过PHP简单地实现,但不能左右我的头。可有人请点什么我可能会丢失?

However I am believe that this can be simply achieved by PHP, but can't get my head around. Can somebody please point what I might be missing ?

注意给版主和SO维吉兰特

请不要把它个人,但标志着甚至说这是重复之前,请仔细阅读我的问题和明白我想实现只有到那时,如果你同意敬请详细解释为什么你认为它重复的,而不是简单地对问题的争论基地具有类似的标题

我已经经历了这些问题了,但他们没有我的情况下工作,因为他们试图合并基于一个特定元素数组,总和值是通常是ID,但对我来说它的独特性判断上的组合元素(而不是一个)

I have gone through these questions, but they don't work in my scenario, as they try to merge array and sum value based on one specific element which is usually either ID, but in my case its uniqueness is judged on combination of elements (not one)

1 ,<一个href=\"http://stackoverflow.com/questions/5821948/in-php-find-duplicate-entry-in-a-multi-dimensional-array-then-sum-the-values-in\">2, 3

推荐答案

如果你绝对要做到这一点在PHP中,则是这样的:

If you absolutely have to do this in PHP, then something like:

$data = array(
    array(
        'shelf' => 'Left',
        'weight' => '10.000',
        'piece' => 1,
    ),
    array(
        'shelf' => 'Right',
        'weight' => '04.000',
        'piece' => 12,
    ),
    array(
        'shelf' => 'Right',
        'weight' => '04.000',
        'piece' => 4,
    ),
    array(
        'shelf' => 'Right',
        'weight' => '07.000',
        'piece' => 8,
    ),
);


$newData = array();
$result = array_reduce(
    $data,
    function($newData, $value) {
        $key = $value['shelf'].$value['weight'];
        if (!isset($newData[$key])) {
            $newData[$key] = $value;
        } else {
            $newData[$key]['piece'] += $value['piece'];
        }
        return $newData;
    },
    $newData
);
var_dump($result);

将工作,但我真的不相信你创建你的整个方法为自己主要的性能问题,这个问题

will work, but I really do believe that you're creating major performance problems for yourself with your whole approach to this problem

这篇关于在PHP中,合并重复集的多维数组的元素,总结特定键的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆