In PHP, merge duplicate set of elements of an mult

2019-03-04 17:28发布

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条回答
我命由我不由天
2楼-- · 2019-03-04 18:23

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

查看更多
登录 后发表回答