与其他索引值的单个多维数组检查php [英] single multidimensional array check with other index values php
本文介绍了与其他索引值的单个多维数组检查php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的数组,我想检查一个索引值以保持其他索引值,如果product_id值相同,则求和total_qty.我处于这种情况.我需要帮助.谢谢您.
This is my array i want to check one index value to remain other index value and if product_id value same then sum of total_qty. i am stuck in this situation.i need help.Thank you in advance.
Array
(
[0] => Array
(
[iProduct_id] => 1
[fTotal_qty] => 200
)
[1] => Array
(
[iProduct_id] => 2
[fTotal_qty] => 400
)
[2] => Array
(
[iProduct_id] => 6
[fTotal_qty] => 500
)
[3] => Array
(
[iProduct_id] => 4
[fTotal_qty] => 300
)
[4] => Array
(
[iProduct_id] => 5
[fTotal_qty] => 200
)
[5] => Array
(
[iProduct_id] => 6
[fTotal_qty] => 200
)
[6] => Array
(
[iProduct_id] => 1
[fTotal_qty] => 300
)
)
我想要这样的输出
product_id = 1
total_qty = 500
product_id = 2
total_qty = 400
product_id = 6
total_qty = 700
product_id = 4
total_qty = 300
product_id = 5
total_qty = 200
推荐答案
您可以使用简单的for-loop
$arr = array(array("id" => "1", "amount" => 200), array("id" => "2", "amount" => 400), array("id" => "1", "amount" => 100), array("id" => "2", "amount" => 600));
$res = array();
foreach($arr as $elem) {
if (!isset($res[$elem["id"]])) //if this is the first time for this ID
$res[$elem["id"]] = 0;
$res[$elem["id"]] += $elem["amount"]; // add current amount
}
现在res
包含(键是product_id
,值是total_qty
):
Now res
contains (key is the product_id
and the value is the total_qty
):
Array
(
[1] => 300
[2] => 1000
)
这篇关于与其他索引值的单个多维数组检查php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文