如何使用公共变量对具有二维的二维数组的行进行分组

发布于 2024-12-17 17:42:27 字数 6251 浏览 0 评论 0原文

我有一个像这样的二维数组,

    $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2024-12-24 17:42:27

我只想在“id_order”列上使用 foreach 和索引:

$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'];
    }
}

I would just use a foreach and index on the "id_order" column:

$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'];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文