我有一个像这样的二维数组
$tab2 = Array
(
0 => Array
(
"id_order" => 1551,
"firstname" => "ggg",
"lastname" =>" ggg",
"email" =>" var",
"ad1" =>" yjtyj",
"ad2" =>" ",
"adpostcode" => "ytjty",
"adcity" => "ytjy",
"adphone" => "cxgdfg",
"adphone_mobile" => "dfgd",
"dstateiso" => "",
"dstate" => "",
"dcountryiso" =>" FR",
"dcountry" => "France",
"aiother" => "",
"product_weight" => 15.02
),
1 => Array
(
"id_order" => 1551,
"firstname" => "ggg",
"lastname" =>" ggg",
"email" =>" var",
"ad1" =>" yjtyj",
"ad2" =>" ",
"adpostcode" => "ytjty",
"adcity" => "ytjy",
"adphone" => "cxgdfg",
"adphone_mobile" => "dfgd",
"dstateiso" => "",
"dstate" => "",
"dcountryiso" =>" FR",
"dcountry" => "France",
"aiother" => "",
"product_weight" => 35.02
),
2 => Array
(
"id_order" => 1551,
"firstname" => "ggg",
"lastname" =>" ggg",
"email" =>" var",
"ad1" =>" yjtyj",
"ad2" =>" ",
"adpostcode" => "ytjty",
"adcity" => "ytjy",
"adphone" => "cxgdfg",
"adphone_mobile" => "dfgd",
"dstateiso" => "",
"dstate" => "",
"dcountryiso" =>" FR",
"dcountry" => "France",
"aiother" => "",
"product_weight" => 43.02
),
3 => Array
(
"id_order" => 1550,
"firstname" => "dsgrg",
"lastname" => "regerzg",
"email" => "erger",
"ad1" => "5regrte",
"ad2" => "",
"adpostcode" => 62460,
"adcity" => "regger",
"adphone" => "",
"adphone_mobile" => "",
"dstateiso" => "",
"dstate" => "",
"dcountryiso" => "FR",
"dcountry" => "France",
"aiother" => "",
"product_weight" => 23.6
),
4 => Array
(
"id_order" => 1550,
"firstname" => "dsgrg",
"lastname" => "regerzg",
"email" => "erger",
"ad1" => "5regrte",
"ad2" => "",
"adpostcode" => 62460,
"adcity" => "regger",
"adphone" => "",
"adphone_mobile" => "",
"dstateiso" => "",
"dstate" => "",
"dcountryiso" => "FR",
"dcountry" => "France",
"aiother" => "",
"product_weight" => 13.6
),
5 => Array
(
"id_order" => 1549,
"firstname" =>" thtr",
"lastname" =>" rthr",
"email" => "lejardindechhhhhhlaire@rthhhhhhhhhfr",
"ad1" =>"chetrhrthrtine",
"ad2" => "",
"adpostcode" => 84120,
"adcity" =>" rthrt",
"adphone" => "",
"adphone_mobile" => 2344235,
"dstateiso" => "",
"dstate" => "",
"dcountryiso" => "FR",
"dcountry" => "France",
"aiother" => "",
"product_weight" => 47.19
)
);
我试图将一个数组中具有相同id_order的数组分组,并根据出现的次数计算product_weight的总数。 我想要的输出就像这样
0 => Array
(
"id_order" => 1551,
"firstname" => "ggg",
"lastname" =>" ggg",
"email" =>" var",
"ad1" =>" yjtyj",
"ad2" =>" ",
"adpostcode" => "ytjty",
"adcity" => "ytjy",
"adphone" => "cxgdfg",
"adphone_mobile" => "dfgd",
"dstateiso" => "",
"dstate" => "",
"dcountryiso" =>" FR",
"dcountry" => "France",
"aiother" => "",
"product_weight" => 93.06 //(total of the 3 occurences 15.02+35.02+43.02)
),
1 => Array
(
"id_order" => 1550,
"firstname" => "dsgrg",
"lastname" => "regerzg",
"email" => "erger",
"ad1" => "5regrte",
"ad2" => "",
"adpostcode" => 62460,
"adcity" => "regger",
"adphone" => "",
"adphone_mobile" => "",
"dstateiso" => "",
"dstate" => "",
"dcountryiso" => "FR",
"dcountry" => "France",
"aiother" => "",
"product_weight" => 37.2
),
2 => Array
(
"id_order" => 1549,
"firstname" =>" thtr",
"lastname" =>" rthr",
"email" => "lejardindechhhhhhlaire@rthhhhhhhhhfr",
"ad1" =>"chetrhrthrtine",
"ad2" => "",
"adpostcode" => 84120,
"adcity" =>" rthrt",
"adphone" => "",
"adphone_mobile" => 2344235,
"dstateiso" => "",
"dstate" => "",
"dcountryiso" => "FR",
"dcountry" => "France",
"aiother" => "",
"product_weight" => 47.19
)
);
我已经尝试过这个脚本,但它没有用
$nbr = sizeof($tab2);
$x=0;
for($i=0;$i<$nbr;$i++){
for($j=$i+1;$j<$nbr;$j++){
if($tab[$i]["id_order"]== $tab[$j]["id_order"])
{
echo 'doublon<br>';
$tab[$i]["product_weight"] = $tab[$i]["product_weight"]+ $tab[$j]["product_weight"];
// unset($tab[$j]);
array_splice($tab,$i);
}
}
}
感谢您的帮助
答案 0 :(得分:0)
我只想在“id_order”列中使用foreach和index:
$newtab = array();
foreach ($tab2 as $i => $tab) {
if (!isset($newtab[$tab['id_order']])) {
$newtab[$tab['id_order']] = $tab;
} else {
$newtab[$tab['id_order']]['product_weight'] += $tab['product_weight'];
}
}