将两列值上的行分组并对特定键的值求和 [英] Group rows on two column values and sum the values of specific key

查看:48
本文介绍了将两列值上的行分组并对特定键的值求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组,我试图将具有 shelfweight 值的元素合并为重复项,并对 piece 的值求和键.

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

目前,我通过创建具有以下字段 shelfweightpiece 的临时表,在 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(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
        )
 )

但是我相信这可以通过 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 ?

版主和特别警员注意

请不要把它个人化,但在标记或什至说这是重复之前,请仔细阅读我的问题并理解我想要实现的目标,只有在您同意的情况下,请详细解释为什么您认为它是重复的重复,而不是简单地根据标题相似的问题争论

我已经解决了这些问题,但它们在我的场景中不起作用,因为它们尝试根据一个特定元素(通常是 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)

12,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

这篇关于将两列值上的行分组并对特定键的值求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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