我必须在相同的id,大小和类型上加入数组数组,并在php中加上num键值。我找到了只有一个键的解决方案,但我需要匹配3个键
Array
(
[3] => Array
(
[id] => 460
[size] => 24
[num] => 1
[price] => 6800.00
[type] => 1
)
[2] => Array
(
[id] => 460
[size] => 24
[num] => 1
[price] => 6800.00
[type] => 1
)
[1] => Array
(
[id] => 460
[size] => 24
[num] => 1
[price] => 6800.00
[type] => 2
)
)
我需要得到如下所示的数组,其中每个都保持不变,只有num字段被求和,其中id,type和size键值相同
阵 (
[2] => Array
(
[id] => 460
[size] => 24
[num] => 2
[price] => 6800.00
[type] => 1
)
[1] => Array
(
[id] => 460
[size] => 24
[num] => 1
[price] => 6800.00
[type] => 2
)
)
答案 0 :(得分:0)
我已经很简单地解决了,使用像这个例子中的临时数组
$newArr = array();
foreach($your_arr as $key=>$val){
$index = $val['context'].$val[1];
if(isset($newArr[$index])){
$val_0 = $newArr[$val['context'].$val[1]][0] + $val[0];
$newArr[$val['context'].$val[1]] = array($val_0, $val[1], 'context'=>$val['context']);
}else{
$newArr[$val['context'].$val[1]] = $val;
}
}
$result = array_values($newArr);
print '<pre>';
print_r($result);
print '</pre>';