In PHP, merge duplicate set of elements of an multidimensional array and sum the values of specific key

感情迁移 提交于 2019-12-31 07:12:10

问题


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
        )
)

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 shelf, weight, SUM(piece) FROM temp GROUP BY CONCAT(shelf, weight)

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
        )
 )

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 ?

Note to Moderators and SO Vigilante

Please don't take it personal but before marking or even saying this is duplicate, read my question thoroughly and understand what I am trying to achieve and only then if you agree kindly explain in detail why do you think its duplicate, rather than simply arguing on base of question with similar title

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, 2, 3


回答1:


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



来源:https://stackoverflow.com/questions/24150063/in-php-merge-duplicate-set-of-elements-of-an-multidimensional-array-and-sum-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!