これは私の配列です。1つのインデックス値をチェックして他のインデックス値を維持したいのですが、product_id値が同じ場合は、total_qtyの合計です。私はこの状況で立ち往生しています。助けが必要です。よろしくお願いします。
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
):
Array
(
[1] => 300
[2] => 1000
)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加