//现有要求如下一个三位数组$shu 把里面的相同的商品对应的数量和价格相加
$shu=Array ( [0] => Array ( [0] => 18 [1] => '产品1' [2] => 1 [3] => 0.01 ) [1] => Array ( [0] => 18 [1] => '产品2' [2] => 1 [3] => 0.01 ) [2] => Array ( [0] => 17 [1] => '产品3' [2] => 1 [3] => 0.01 ) [3] => Array ( [0] => 19 [1] => '产品4' [2] => 1 [3] => 0.01 ) [4] => Array ( [0] => 16 [1] => '00' [2] => 1 [3] => 100 ) [5] => Array ( [0] => 18 [1] => '产品5' [2] => 1 [3] => 0.01 ) );
sort($shu);
$tem = "";
$temarray = array();
$j = 0;
for($i=0;$i<count($shu);$i++)
{
if($shu[$i][0]==$tem[0])
{
if(($j-1)<0){$x=0;}else{$x=$j-1;};
$temarray[$x][2]= $temarray[$x][2]+$shu[$i][2];
$temarray[$x][3]= $temarray[$x][3]+$shu[$i][3];
}else{
$temarray[$j]=$shu[$i];
//print_r($temarray);
$j++;
$tem = $shu[$i];
}
}
//print_r($temarray);die();
}
多维数组 对应的产品数量和价格相加
最新推荐文章于 2022-04-25 10:21:13 发布