2 回答
TA贡献1936条经验 获得超6个赞
这是你可以做的片段,
$nsdIds = [];
$result = [];
foreach ($qp as $key => $value) {
// exploding Nsd_ids
$nsdTemp = explode("@", $value['Nsd_id']);
// exploding nsd_amounts
$nsdAmount = explode("@", $value['Nsd_amount']);
// saving all new nsd ids and only keep unique nsd ids
$nsdIds = array_unique(array_merge($nsdIds, $nsdTemp));
// I am now combining keeping nsd id as key and nsd amount as its value
$temp1 = array_combine($nsdTemp, $nsdAmount);
foreach ($nsdIds as $value1) {
// for latest php version
// $temp1 contains keys of nsd ids
// value1 contains nsdids we are sure that it will be in nsdids as we are already merging those ids
// then fetching amounf for that nsdids
//$result[$value1] = ($result[$value1] ?? 0) + $temp1[$value1];
// for php version less than 7
$result[$value1] = (!empty($result[$value1]) ? $result[$value1] : 0) +
(!empty($temp1[$value1]) ? $temp1[$value1] : 0);
}
}
print_r($result);die;
TA贡献1776条经验 获得超12个赞
只要nsd_id和nsd_amount在同一行中具有相同数量的值,@那么这应该可以工作。如果没有,那么您需要isset检查$nsd_amount[$key]:
foreach($qp as $luna) {
$nsd_id = explode("@", $luna['nsd_id']);
$nsd_amount = explode("@", $luna['nsd_amount']);
foreach($nsd_id as $key => $id) {
if(isset($result[$id])) {
$result[$id] += $nsd_amount[$key];
} else {
$result[$id] = $nsd_amount[$key];
}
}
}
- 2 回答
- 0 关注
- 148 浏览
添加回答
举报