如何使用公共变量对具有二维的二维数组的行进行分组
我有一个像这样的二维数组,
$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);
}
}
}
感谢您的帮助
i have a 2 dimension array like this one
$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
)
);
im trying to group the array who have the same id_order in one array and count the total of the product_weight depending of the number of occurences.
the output that i want is like this
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
)
);
I have tried this script but it didnt work
$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);
}
}
}
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只想在“id_order”列上使用 foreach 和索引:
I would just use a foreach and index on the "id_order" column: