数组基于相同键的值总和

时间:2010-07-20 03:14:52

标签: php arrays sum

我的数组数据如下所示:

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*'

6 个答案:

答案 0 :(得分:8)

因此,首先您需要$amountsArray以某种方式分配您列出的值。然后:

$bankTotals = array();
foreach($amountsArray as $amount)
{
  $bankTotals[$amount['name']] += $amount['amount'];
}

在此之后,$bankTotals是一个以银行名称索引的数组,其中包含银行总金额的值。你可以从这里使用这个数组。

可能有用的一件事是另一个foreach循环打印出来:

foreach($bankTotals as $name => $amount)
{
  echo $name.".....".$amount."\n";
}

答案 1 :(得分:6)

<?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;
}

答案 2 :(得分:2)

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);
}

答案 3 :(得分:2)

我宁愿添加

$bankTotals = array();
foreach($amountsArray as $amount)
{
 if(isset($bankTotals[$amount['name']]))
    $bankTotals[$amount['name']] += $amount['amount'];
 else
    $bankTotals[$amount['name']] = $amount['amount'];
}

答案 4 :(得分:0)

$a = arrayofindonesianbanks;

foreach ($a as $anarrays) {
        echo "$anarrays[name]."  ".$anarrays[amount]";
    }
}

在php中查看foreach。

答案 5 :(得分:0)

@Shubham 的循环是一种非常合适的技术。

如果您愿意,您可能希望使用 array_reduce()

  • 避免生成全局范围的变量或
  • 想要使用返回值或
  • 通常更喜欢函数式风格

空合并运算符防止生成任何警告/错误添加到结果数组中尚未声明的元素

代码:(Demo)

var_export(
    array_reduce(
        $array,
        function($carry, $row) {
            $carry[$row['name']] = ($carry[$row['name']] ?? 0) + $row['amount'];
            return $carry;
        }
    )
);

输出:

array (
  'Bank BRI' => 34534534,
  'Bank BCA' => 1435773657,
  'Bank CIMB Niaga' => 1338303418,
  'Bank BNI' => 124124,
  'Bank Mandiri' => 0,
  'Bank Permata' => 352352353,
)