我有一个如下所示的数组。来自字段是该人的帐号,第二个字段是金额,
第三个字段到是钱的人的帐号。那么我该如何计算每个账号的当前余额。
Array
(
[0] => Array
(
[from] => 314
[amount] => $470.21
[to] => 275
)
[1] => Array
(
[from] => 12
[amount] => $1,788.98
[to] => 149
)
[2] => Array
(
[from] => 316
[amount] => $2,949.53
[to] => 417
)
[3] => Array
(
[from] => 5
[amount] => $2,193.48
[to] => 454
)
[4] => Array
(
[from] => 198
[amount] => $1,402.76
[to] => 371
)
[5] => Array
(
[from] => 82
[amount] => $1,212.14
[to] => 420
)
[6] => Array
(
[from] => 222
[amount] => $1,167.72
[to] => 396
)
[7] => Array
(
[from] => 348
[amount] => $4,024.42
[to] => 399
)
[8] => Array
(
[from] => 474
[amount] => $1,216.86
[to] => 60
)
[9] => Array
(
[from] => 449
[amount] => $974.59
[to] => 422
)
[10] => Array
(
[from] => 415
[amount] => $232.12
[to] => 351
)
[11] => Array
(
[from] => 164
[amount] => $353.19
[to] => 42
)
[12] => Array
(
[from] => 321
[amount] => $4,202.13
[to] => 377
)
[13] => Array
(
[from] => 99
[amount] => $1,178.71
[to] => 51
)
[14] => Array
(
[from] => 251
[amount] => $3,015.86
[to] => 262
)
[15] => Array
(
[from] => 374
[amount] => $2,014.48
[to] => 299
)
[16] => Array
(
[from] => 351
[amount] => $2,807.71
[to] => 302
)
[17] => Array
(
[from] => 286
[amount] => $2,516.63
[to] => 166
)
[18] => Array
(
[from] => 110
[amount] => $464.08
[to] => 385
)
[19] => Array
(
[from] => 171
[amount] => $1,623.41
[to] => 452
)
[20] => Array
(
[from] => 293
[amount] => $727.26
[to] => 208
)
[21] => Array
(
[from] => 373
[amount] => $2,200.46
[to] => 499
)
[22] => Array
(
[from] => 194
[amount] => $1,393.01
[to] => 258
)
[23] => Array
(
[from] => 315
[amount] => $764.34
[to] => 487
)
[24] => Array
(
[from] => 199
[amount] => $586.14
[to] => 48
)
[25] => Array
(
[from] => 67
[amount] => $402.24
[to] => 59
)
[26] => Array
(
[from] => 481
[amount] => $6,551.75
[to] => 400
)
[27] => Array
(
[from] => 154
[amount] => $1,076.45
[to] => 193
)
[28] => Array
(
[from] => 431
[amount] => $577.47
[to] => 196
)
[29] => Array
(
[from] => 408
[amount] => $385.03
[to] => 153
)
[30] => Array
(
[from] => 240
[amount] => $532.56
[to] => 92
)
[31] => Array
(
[from] => 195
[amount] => $60.00
[to] => 361
)
[32] => Array
(
[from] => 425
[amount] => $438.85
[to] => 417
)
[33] => Array
(
[from] => 309
[amount] => $2,222.94
[to] => 131
)
[34] => Array
(
[from] => 366
[amount] => $732.07
[to] => 76
)
[35] => Array
(
[from] => 492
[amount] => $891.88
[to] => 342
)
[36] => Array
(
[from] => 384
[amount] => $1,936.74
[to] => 414
)
[37] => Array
(
[from] => 83
[amount] => $270.87
[to] => 427
)
[38] => Array
(
[from] => 43
[amount] => $1,100.84
[to] => 202
)
[39] => Array
(
[from] => 428
[amount] => $1,825.57
[to] => 387
)
[40] => Array
(
[from] => 238
[amount] => $1,059.79
[to] => 381
)
[41] => Array
(
[from] => 426
[amount] => $416.23
[to] => 97
)
[42] => Array
(
[from] => 190
[amount] => $7,057.28
[to] => 238
)
[43] => Array
(
[from] => 229
[amount] => $722.97
[to] => 159
)
[44] => Array
(
[from] => 129
[amount] => $785.86
[to] => 303
)
[45] => Array
(
[from] => 44
[amount] => $2,961.07
[to] => 105
)
[46] => Array
(
[from] => 306
[amount] => $893.92
[to] => 178
)
)
答案 0 :(得分:0)
您可以创建一个按帐号编入索引的$ balance数组。然后在for循环中遍历原始数组。
在每个元素中,您可以减去“金额”'来自'索引的$ balance余额并添加'金额'以'到'索引的平衡。
答案 1 :(得分:0)
好吧,我自己做了,这是解决方案
function checkBalance($array){
$accounts = [];
if(is_array($array) && !empty($array)){
foreach($array as $value){
$amount = preg_replace('/[^\d.]/', '', $value['amount']);
$account_from = $value['from'];
$account_to = $value['to'];
$transaction_amount = $amount;
if (isset($accounts[$account_from])){
$accounts[$account_from] -= $transaction_amount;
}
else{
$accounts[$account_from] = (0 - $transaction_amount);
}
if (isset($accounts[$account_to])){
$accounts[$account_to] += $transaction_amount;
}
else{
$accounts[$account_to] = $transaction_amount;
}
}
}
return $accounts;
}
print_r(checkBalance($array));
是的,感谢所有回复的人:)