![](/img/trans.png)
[英]PHP: Group (multidimensional, associative) array and sum values by specific key
[英]PHP getting sum of values group by key in 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
)
)
我想遍歷這些記錄並通過 product_id 獲得總重量和凈重量組的總和
我嘗試了下面的代碼,但無法從這里繼續獲取輸出
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;
}
我想要的輸出是在新數組中獲取每個產品 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;
}
使用 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);
結果:
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.