简体   繁体   English

PHP 通过数组中的键获取值的总和

[英]PHP getting sum of values group by key in array

i have bellow array我有波纹管阵列

 Array
(
[0] => stdClass Object
    (
        [gross_weight] => 20.500
        [net_weight] => 10.500
        [product_id] => 1120280

    )

[1] => stdClass Object
    (
        [gross_weight] => 20.500
        [net_weight] => 10.500     
        [product_id] => 1120281

    )

[2] => stdClass Object
    (
        [gross_weight] => 20.500
        [net_weight] => 10.500      
        [product_id] => 1120281

    )

[3] => stdClass Object
    (
        [gross_weight] => 20.500
        [net_weight] => 10.500
        [product_id] => 1120280
    )
)

i want loop through this records and get the sum of gross_weight and net_weight group by product_id我想遍历这些记录并通过 product_id 获得总重量和净重量组的总和

i tried bellow code but not able to continue from here get the out put我尝试了下面的代码,但无法从这里继续获取输出

foreach ($my_array as $my_array_data) {

            $mark_product_id = $my_array_data->product_id;
            $gross_weight_mt = $mrkgdata->gross_weight_mt;
            $net_weight_mt = $mrkgdata->net_weight_mt;            
    }

my desired out put is to get the gross_weight and net_weight for each product id in new array.我想要的输出是在新数组中获取每个产品 id 的总重量和净重量。

Array
(
[0] => stdClass Object
(
    [gross_weight] => 41.000
    [net_weight] => 21.000
    [product_id] => 1120280

)

[1] => stdClass Object
(
    [gross_weight] => 41.000
    [net_weight] => 21.000     
    [product_id] => 1120281

)
)
public function getGroupByData($data) {
    $groups = array();
    foreach ($data as $item) {
        $key = $item['product_id'];
        if (!array_key_exists($key, $groups)) {
            $groups[$key] = array(
                'id' => $item['product_id'],
                'gross_weight' => $item['gross_weight'],
                'net_weight' => $item['net_weight'],
            );
        } else {
            $groups[$key]['gross_weight'] = $groups[$key]['gross_weight'] + $item['gross_weight'];
            $groups[$key]['net_weight'] = $groups[$key]['net_weight'] + $item['net_weight'];
        }
    }
    return $groups;
}

use php array as dictionary, and then just add the gross_weight and net_weight with += operator使用 php 数组作为字典,然后使用+=运算符添加 Gross_weight 和 net_weight

$json = '
[
    {
        "gross_weight" : 20500,
        "net_weight" : 10500,
        "product_id" : 1120280

    },
    {
        "gross_weight" : 20500,
        "net_weight" : 10500,     
        "product_id" : 1120281

    },
    {
        "gross_weight" : 20500,
        "net_weight" : 10500,      
        "product_id" : 1120281
    },
    {
        "gross_weight" : 20500,
        "net_weight" : 10500,
        "product_id" : 1120280
    }
]
';
$x = json_decode($json);
var_dump($x);

$total = array();
foreach($x as $key => $val) {
    if(empty($total[$val->product_id])) $total[$val->product_id] = array( "net_weight"=>0, "gross_weight"=>0, "product_id"=>$val->product_id );
    $total[$val->product_id]["net_weight"] += $val->net_weight;
    $total[$val->product_id]["gross_weight"] += $val->gross_weight;
}
$result = array_values($total);
var_dump($result);

result:结果:

array(4) {
  [0]=>
  object(stdClass)#8 (3) {
    ["gross_weight"]=>
    int(20500)
    ["net_weight"]=>
    int(10500)
    ["product_id"]=>
    int(1120280)
  }
  [1]=>
  object(stdClass)#7 (3) {
    ["gross_weight"]=>
    int(20500)
    ["net_weight"]=>
    int(10500)
    ["product_id"]=>
    int(1120281)
  }
  [2]=>
  object(stdClass)#6 (3) {
    ["gross_weight"]=>
    int(20500)
    ["net_weight"]=>
    int(10500)
    ["product_id"]=>
    int(1120281)
  }
  [3]=>
  object(stdClass)#5 (3) {
    ["gross_weight"]=>
    int(20500)
    ["net_weight"]=>
    int(10500)
    ["product_id"]=>
    int(1120280)
  }
}
array(2) {
  [0]=>
  array(3) {
    ["net_weight"]=>
    int(21000)
    ["gross_weight"]=>
    int(41000)
    ["product_id"]=>
    int(1120280)
  }
  [1]=>
  array(3) {
    ["net_weight"]=>
    int(21000)
    ["gross_weight"]=>
    int(41000)
    ["product_id"]=>
    int(1120281)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM