У меня есть данные массива, как показано ниже:
Array ( [0] => Array ( [name] => Bank BRI [amount] => 0 ) [1] => Array ( [name] => Bank BRI [amount] => 0 ) [2] => Array ( [name] => Bank BCA [amount] => 1412341234 ) [3] => Array ( [name] => Bank CIMB Niaga [amount] => 532532552 ) [4] => Array ( [name] => Bank BRI [amount] => 34534534 ) [5] => Array ( [name] => Bank CIMB Niaga [amount] => 453425243 ) [6] => Array ( [name] => Bank BRI [amount] => 0 ) [7] => Array ( [name] => Bank BNI [amount] => 124124 ) [8] => Array ( [name] => Bank CIMB Niaga [amount] => 352345623 ) [9] => Array ( [name] => Bank BCA [amount] => 23432423 ) [10] => Array ( [name] => Bank Mandiri [amount] => 0 ) [11] => Array ( [name] => Bank BCA [amount] => 0 ) [12] => Array ( [name] => Bank BCA [amount] => 0 ) [13] => Array ( [name] => Bank Permata [amount] => 352352353 ) )
Как суммировать сумму, основанную на том же «имени банка».
Мои данные должны отображаться следующим образом:
Bank BRI........... with '*total amount*' Bank BCA........... with '*total amount*' Bank CIMB NIAGA.... with '*total amount*' Bank BNI........... with '*total amount*' Bank Mandiri....... with '*total amount*' Bank Permata....... with '*total amount*'
<?php // array of bank structure $banks = array(); $banks[] = array('name'=>'Bank BRI','amount'=>rand()); $banks[] = array('name'=>'Bank BRI','amount'=>rand()); $banks[] = array('name'=>'Bank BCA','amount'=>rand()); $banks[] = array('name'=>'Bank CIMB','amount'=>rand()); $banks[] = array('name'=>'Bank BRI','amount'=>rand()); $banks[] = array('name'=>'Bank CIMB','amount'=>rand()); $banks[] = array('name'=>'Bank BRI','amount'=>rand()); $banks[] = array('name'=>'Bank BNI','amount'=>rand()); $banks[] = array('name'=>'Bank CIMB','amount'=>rand()); $banks[] = array('name'=>'Bank BCA','amount'=>rand()); $banks[] = array('name'=>'Bank Mandiri','amount'=>rand()); $banks[] = array('name'=>'Bank BCA','amount'=>rand()); $banks[] = array('name'=>'Bank BCA','amount'=>rand()); $banks[] = array('name'=>'Bank Permata','amount'=>rand()); // begin the iteration for grouping bank name and calculate the amount $amount = array(); foreach($banks as $bank) { $index = bank_exists($bank['name'], $amount); if ($index < 0) { $amount[] = $bank; } else { $amount[$index]['amount'] += $bank['amount']; } } print_r($amount); //display // for search if a bank has been added into $amount, returns the key (index) function bank_exists($bankname, $array) { $result = -1; for($i=0; $i<sizeof($array); $i++) { if ($array[$i]['name'] == $bankname) { $result = $i; break; } } return $result; }
Итак, сначала вам нужно $amountsArray
чтобы каким-то образом присвоить значения, которые вы указали. Затем:
$bankTotals = array(); foreach($amountsArray as $amount) { $bankTotals[$amount['name']] += $amount['amount']; }
После этого $bankTotals
представляет собой массив, проиндексированный по имени банка, со значением общей суммы для банка. Вы можете использовать этот массив, как вы считаете нужным.
Одна вещь, которая может быть полезна, – это еще один цикл foreach
чтобы распечатать все это:
foreach($bankTotals as $name => $amount) { echo $name.".....".$amount."\n"; }
Пример в C #:
Dictionary<string,object>[] items = { new Dictionary<string, object> {{ "name", "Bank BRI"}, {"amount", 0 }}, new Dictionary<string, object> {{ "name", "Bank BRI"}, {"amount", 0 }}, new Dictionary<string, object> {{ "name", "Bank BCA"}, {"amount", 1412341234 }}, new Dictionary<string, object> {{ "name", "Bank CIMB Niaga"}, {"amount", 532532552 }} }; var amounts = new Dictionary<string, int>(); foreach (var item in items) { string bank = (string)item["name"]; int amount = (int)item["amount"]; if (amounts.ContainsKey(bank)) { amounts[bank] += amount; } else { amounts.Add(bank, amount); } } foreach (var amount in amounts) { Console.WriteLine("{0}: {1}", amount.Key, amount.Value); }
Я бы предпочел добавить
$bankTotals = array(); foreach($amountsArray as $amount) { if(isset($bankTotals[$amount['name']])) $bankTotals[$amount['name']] += $amount['amount']; else $bankTotals[$amount['name']] = $amount['amount']; }
$a = arrayofindonesianbanks; foreach ($a as $anarrays) { echo "$anarrays[name]." ".$anarrays[amount]"; } }
см. foreach в php.