我有一个附加到amount
的{{1}}个(基于$$$销售额)的数组(其中一些id
是基于谁做的id
一笔销售)。我的目标是收集总共amount
个,并将每个总数附加到出售的id
中(从数组中截断相同的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...
}
}
答案 0 :(得分:3)
对于我来说,您的代码看起来很好,对于评论来说,这是一个简单的
$operSums[$value['id']] = $value['amount'];
应该做的伎俩。当然,建议检查现有密钥,但
foreach ($operearnedArray as $key => $value) {
if($value['id'] == '' || $value['id'] == null) {
continue;
}
$operSums[$value['id']] += $value['amount'];
}
应该也可以正常工作,正如您在this demo中看到的那样。
答案 1 :(得分:0)
尝试:
while ($row) {
$operearnedArray[] = array(
'amount' => $row['ChargeAmount'],
'id' => $row['OperatorID']);
}
$operSums = array();
foreach ( $operearnedArray as $sale ) {
if ( ! $sale['id'] ) {
continue;
}
$operSums[$sale['id']] += $sale['amount'];
}
// do something with the result, let's just look at it:
var_dump( $operSums );