gpt4 book ai didi

php - 使用 array_key_exists 将总金额与 id 配对

转载 作者:行者123 更新时间:2023-11-29 05:20:25 24 4
gpt4 key购买 nike

我有一个数组,其中有几个 amount(基于 $$$ 销售额)附加到 id(其中一些 ids 是相同的,基于谁进行了销售)。我的目标是收集总计 amount 并将每个总计附加到进行销售的任何 id ).老实说,我不确定如何去做,因为我还在学习。

while ($row) {
$operearnedArray[] = array(
'amount' => $row['ChargeAmount'],
'id' => $row['OperatorID']);
}
//$operearnedArray returns the array with each "amount" attached to "id"
foreach ($operearnedArray as $key => $value) {
if($value['id'] == '' || $value['id'] == null) {
continue;
}
if(array_key_exists($value['id'], $operSums)) {
$operSums[$value['id']] += $value['amount'];
} else {
//I'm not sure where to go from here...
}
}

最佳答案

我觉得你的代码很好,至于评论,一个简单的

$operSums[$value['id']] = $value['amount'];

应该可以解决问题。当然,建议检查是否存在 key ,但是

foreach ($operearnedArray as $key => $value) {
if($value['id'] == '' || $value['id'] == null) {
continue;
}
$operSums[$value['id']] += $value['amount'];
}

应该也能正常工作,正如您在 this demo 中看到的那样.

关于php - 使用 array_key_exists 将总金额与 id 配对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26663607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com