本文介绍了基于相同的密钥值之阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的阵列数据如下图所示:
阵列(
[0] =>阵列([名称] =>银行BRI [金额] = 0)
[1] =>阵列([名称] =>银行BRI [金额] = 0)
[2] =>阵列([名称] =>银行BCA [金额] => 1412341234)
[3] =>阵列([名称] =>银行CIMB NIAGA [金额] => 532532552)
[4] =>阵列([名称] =>银行BRI [金额] => 34534534)
[5] =>阵列([名称] =>银行CIMB NIAGA [金额] => 453425243)
[6] =>阵列([名称] =>银行BRI [金额] = 0)
[7] =>阵列([名称] =>银行BNI [金额] => 124124)
[8] =>阵列([名称] =>银行CIMB NIAGA [金额] => 352345623)
[9] =>阵列([名称] =>银行BCA [金额] => 23432423)
[10] =>阵列([名称] =>曼迪利银行[金额] = 0)
[11] =>阵列([名称] =>银行BCA [金额] = 0)
[12] =>阵列([名称] =>银行BCA [金额] = 0)
[13] =>阵列([名称] =>银行PERMATA [金额] => 352352353)
)
如何基于相同的银行名称'总结'量'。
我的数据应显示是这样的:
银行BRI ...........以*总金额*'
银行BCA ...........以*总金额*'
银行CIMB NIAGA ....有'*总金额*'
BNI银行与...........*总金额*'
曼迪利银行.......用*总金额*'
银行PERMATA .......用*总金额*'
解决方案
< PHP//银行结构数组
$银行=阵列();
$银行[] =阵列('名'=>'银行BRI','量'=> RAND());
$银行[] =阵列('名'=>'银行BRI','量'=> RAND());
$银行[] =阵列('名'=>'银行BCA','量'=> RAND());
$银行[] =阵列('名'=>'银行CIMB','量'=> RAND());
$银行[] =阵列('名'=>'银行BRI','量'=> RAND());
$银行[] =阵列('名'=>'银行CIMB','量'=> RAND());
$银行[] =阵列('名'=>'银行BRI','量'=> RAND());
$银行[] =阵列('名'=>'BNI银行','量'=> RAND());
$银行[] =阵列('名'=>'银行CIMB','量'=> RAND());
$银行[] =阵列('名'=>'银行BCA','量'=> RAND());
$银行[] =阵列('名'=>'曼迪利银行','量'=> RAND());
$银行[] =阵列('名'=>'银行BCA','量'=> RAND());
$银行[] =阵列('名'=>'银行BCA','量'=> RAND());
$银行[] =阵列('名'=>'银行PERMATA','量'=> RAND());//开始迭代分组银行名称和计算量
$金额=阵列();
的foreach($银行为$银行){
$指数= bank_exists($行['名'],$金额);
如果($指数℃下){
$量[] = $银行;
}
其他{
$数额[$指数] ['量'] + = $行['量'];
}
}
的print_r($金额); //显示//搜索如果银行已经加入到$量,返回键(索引)
功能bank_exists($ BANKNAME,$阵列){
$结果= -1;
为($ I = 0; $ I<的sizeof($数组); $ I ++){
如果($数组[$ i] ['名'] == $ BANKNAME){
$结果= $ I;
打破;
}
}
返回$结果;
}
I have my array data as shown below:
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 )
)
How to sum 'amount' based on same 'bank name'.
My data should shown like this:
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;
}
这篇关于基于相同的密钥值之阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!