我正在wordpress中工作并尝试获取订单详细信息,但问题是我有多个具有相同名称的订单,我想获取具有唯一产品名称的产品数量总和,请帮忙。
这是输出
Array
(
[0] => Array
(
[product_name] => demo product
[quantity] => 1
[total] => 5
)
[1] => Array
(
[product_name] => demo product
[quantity] => 1
[total] => 5
)
[2] => Array
(
[product_name] => demo product
[quantity] => 1
[total] => 5
)
[3] => Array
(
[product_name] => test 2nd
[quantity] => 1
[total] => 5
)
[4] => Array
(
[product_name] => 7" DELICIOUS WHITE PIZZA BOX E-FL 1x100
[quantity] => 1
[total] => 6
)
[5] => Array
(
[product_name] => 1000ml MICROWAVE CONTAINER LIDS 1x250
[quantity] => 1
[total] => 19
)
[6] => Array
(
[product_name] => 1000ml MICROWAVE CONTAINER LIDS 1x250
[quantity] => 5
[total] => 95
)
[7] => Array
(
[product_name] => 7" DELICIOUS WHITE PIZZA BOX E-FL 1x100
[quantity] => 20
[total] => 120
)
[8] => Array
(
[product_name] => 7" DELICIOUS WHITE PIZZA BOX E-FL 1x100
[quantity] => 20
[total] => 120
)
[9] => Array
(
[product_name] => 7" DELICIOUS WHITE PIZZA BOX E-FL 1x100
[quantity] => 20
[total] => 120
)
)
下面是我之前尝试过的PHP代码,它可以工作,但我还需要数量的总和
$final_data[] = array(
'product_name' => $product_name,
'quantity' => $item_quantity,
'total' => number_format($item_total)
);
$output_data = array_map("unserialize", array_unique(array_map("serialize", $final_data)));
任何解决方案表示赞赏
答案 0 :(得分:0)
尝试此操作,它将为您提供基于产品名称的数组,可以帮助您获取正确的值。
$new_data = array();
Foreach($data as $key=>$value){
$new_data[$value['product_name']][] = $value;
}
print_r($new_data);
答案 1 :(得分:0)
去那里
$totalQuantity = array_sum(array_column($array, 'quantity'));
$total = array_sum(array_column($array, 'total'));
希望对您有帮助
编辑
我不确定您要给您的产品起什么样的名字,因为它们似乎对我来说都不一样,因此您可以为此制定自己的逻辑。