我必须根据TXNLineNo从给定的关联数组中获取Amount字段的总和。
Array
(
[0] => Array
(
[TXNID] => 0
[RefTXNID] => 1006
[RefTXNObjectType] => 101
[tmpTXNName] => CN-33
[tmpTXNDate] => 2014-08-26
[RefTXNLineNo] => 0
[tmpOpenAmount] => 0
[tmpOpenAmt] => -500.000
[tmpTXNAmount] => 500
[tmpNarration] => Being amount paid to supplier
[Amount] => 500
[TXNCurrencyID] => 213
[ExchangeRate] => 1.00000
[RevisionNumber] => 1
[tmpRevisionNumber] => 1
[RowState] => 435
[tmpContactID] => 948
[tmpAccountID] => 303
[tmpBranchID] => 156
[TXNLineNo] => 1
)
[1] => Array
(
[TXNID] => 0
[RefTXNID] => 983
[RefTXNObjectType] => 84
[tmpTXNName] => RTY-01
[tmpTXNDate] => 2014-08-26
[RefTXNLineNo] => 1
[tmpOpenAmount] => 0
[tmpOpenAmt] => -1000.000
[tmpTXNAmount] => 1000
[tmpNarration] =>
[Amount] => 1000
[TXNCurrencyID] => 213
[ExchangeRate] => 1.00000
[RevisionNumber] => 1
[tmpRevisionNumber] => 1
[RowState] => 435
[tmpContactID] => 948
[tmpAccountID] => 303
[tmpBranchID] => 156
[TXNLineNo] => 1
)
[2] => Array
(
[TXNID] => 0
[RefTXNID] => 1006
[RefTXNObjectType] => 101
[tmpTXNName] => CN-33
[tmpTXNDate] => 2014-08-26
[RefTXNLineNo] => 0
[tmpOpenAmount] => 0
[tmpOpenAmt] => -500.000
[tmpTXNAmount] => 500
[tmpNarration] => Being amount paid to supplier
[Amount] => 500
[TXNCurrencyID] => 213
[ExchangeRate] => 1.00000
[RevisionNumber] => 1
[tmpRevisionNumber] => 1
[RowState] => 435
[tmpContactID] => 948
[tmpAccountID] => 303
[tmpBranchID] => 156
[TXNLineNo] => 2
)
[3] => Array
(
[TXNID] => 0
[RefTXNID] => 983
[RefTXNObjectType] => 84
[tmpTXNName] => RTY-01
[tmpTXNDate] => 2014-08-26
[RefTXNLineNo] => 1
[tmpOpenAmount] => 0
[tmpOpenAmt] => -1000.000
[tmpTXNAmount] => 1000
[tmpNarration] =>
[Amount] => 1000
[TXNCurrencyID] => 213
[ExchangeRate] => 1.00000
[RevisionNumber] => 1
[tmpRevisionNumber] => 1
[RowState] => 435
[tmpContactID] => 948
[tmpAccountID] => 303
[tmpBranchID] => 156
[TXNLineNo] => 2
)
)
我已经使用以下脚本来获取Amount
字段
$ret = array_sum(array_map(function ($i) { return $i['Amount']; }, $data));
但这不适合我。我必须获取金额TxnLineNo Wise
...
答案 0 :(得分:0)
您可以尝试以下
/**
* @param $arr -> Your array
* @param $sum_ind -> which index value you want to sum(eg, Amount)
* @param $check_ind -> checking index (eg, TXNLineNo)
* @param $check_val -> Checking index value (eg,. 1, 2, ....)
* @return int -> Expected summation
*/
function getSumByIndex($arr, $sum_ind, $check_ind, $check_val){
$sum = 0;
foreach($arr as $val){
if($val[$check_ind] == $check_val){
$sum += $val[$sum_ind];
}
}
return $sum;
}
答案 1 :(得分:0)
也许你可以使用这样的东西?
$amounts = array();
for($TxnLineNo = 0; $TxnLineNo < 3; $TxnLineNo++) {
$amounts[$TxnLineNo] = array_sum(array_map(function ($i) { global $TxnLineNo; return ($i['TXNLineNo']==$TxnLineNo)?$i['Amount']:0; }, $data));
}
var_dump($amounts);
或者,如果你想要一个特定的TxnLineNo,只需(例如2):
$ret = array_sum(array_map(function ($i) { return ($i['TXNLineNo']==2)?$i['Amount']:0; }, $data));
答案 2 :(得分:0)
因为你只想总结一件事,你可以这样做一个简单的循环:
$sum = 0;
foreach ($whatever_your_array_is_called as $txtline_arr) {
$sum += $txtline_arr['Amount'];
}