且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

结合在PHP多维数组值

更新时间:2023-02-23 09:33:18

我认为这将工作:

$final=array(); // the final array
$count=array(); // keeps track of instances of each description
$loops=count($array);
for($a=0;$a<$loops;$a++){
    foreach($array[$a] as $s){ //loop through child arrays
        if($count[$s['description']]>0){ //check if description exists in $count
            foreach($final as $k=>$v){ //add sums to the final if it does exist
                if($final[$k]['description']==$s['description']){$final[$k]['shipping-amount']+=$s['shipping-amount'];}
            }
        }else{ //if it doesn't exist in the count array, add it to the final array
            $final[]=$s;
        }
        $count[$s['description']]++;//update the count array
    }
}
//Unset singletons, using the count array
foreach($count as $k=>$v){
    if($v==1){
        foreach($final as $key=>$val){
            if($final[$key]['description']==$k){unset($final[$key]);}
        }
    }
}
print_r($final);

我一直停留在某个问题上,在过去2天,感觉你,所以我希望这有助于。

I have been stuck on an issue for the past 2 days and feel you, so I hope this helps.