php 对一列中的数组数据进行分组,并对另一列中的数据进行求和

jbose2ul  于 2023-01-08  发布在  PHP
关注(0)|答案(5)|浏览(149)

我的阵列数据如下所示:

$array = [
    ['name' => 'Bank BRI', 'amount' => 0], 
    ['name' => 'Bank BRI', 'amount' => 0], 
    ['name' => 'Bank BCA', 'amount' => 1412341234],
    ['name' => 'Bank CIMB Niaga', 'amount' => 532532552], 
    ['name' => 'Bank BRI', 'amount' => 34534534], 
    ['name' => 'Bank CIMB Niaga', 'amount' => 453425243], 
    ['name' => 'Bank BRI', 'amount' => 0], 
    ['name' => 'Bank BNI', 'amount' => 124124], 
    ['name' => 'Bank CIMB Niaga', 'amount' => 352345623], 
    ['name' => 'Bank BCA', 'amount' => 23432423], 
    ['name' => 'Bank Mandiri', 'amount' => 0], 
    ['name' => 'Bank BCA', 'amount' => 0], 
    ['name' => 'Bank BCA', 'amount' => 0], 
    ['name' => 'Bank Permata', 'amount' => 352352353],
];

如何根据相同的“银行名称”合计“金额”。
我的结果应该显示分组名称及其合计金额:

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

alen0pnh1#

因此,首先需要以某种方式为$amountsArray赋值,然后:

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

输出:(Demo

Warning: Undefined array key "Bank BRI"

Warning: Undefined array key "Bank BCA"

Warning: Undefined array key "Bank CIMB Niaga"

Warning: Undefined array key "Bank BNI"

Warning: Undefined array key "Bank Mandiri"

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

之后,$bankTotals是一个以银行名称为索引的数组,该数组的值是银行的总金额,您可以在这里使用该数组。
另一个foreach循环可能会很有用,它可以将所有内容打印出来:

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

jhdbpxl92#

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

agxfikkp3#

我想补充一句

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

o2gm4chl4#

@Shubham的循环是一个非常合适的技术。
如果您希望实现以下目的,您可能希望使用array_reduce()

  • 避免生成全局作用域变量或
  • 要使用返回值或
  • 我一般喜欢实用型的

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

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

或者来自PHP7.4,带有箭头函数语法:(Demo

var_export(
    array_reduce(
        $array,
        fn($carry, $row) => array_merge($carry, [$row['name'] => ($carry[$row['name']] ?? 0) + $row['amount']]),
        // or fn($carry, $row) => $carry + [$row['name'] => ($carry[$row['name']] ?? 0) + $row['amount']],
        []
    )
);

输出:

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

a11xaf1n5#

$a = arrayofindonesianbanks;

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

参见php中的foreach。

相关问题