Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用公共变量对具有第二维的二维数组行进行分组_Php_Arrays_Multidimensional Array_Sorting - Fatal编程技术网

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

Php 如何使用公共变量对具有第二维的二维数组行进行分组,php,arrays,multidimensional-array,sorting,Php,Arrays,Multidimensional Array,Sorting,我有一个像这样的二维数组 $tab2 = Array ( 0 => Array ( "id_order" => 1551, "firstname" => "ggg", "lastname" =>" ggg", "email" =>" var", "ad1" =>" yjtyj", "ad

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

    $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顺序的数组分组到一个数组中,并根据出现的次数计算产品的总重量。 我想要的输出是这样的

    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);
        }
    }
 }

感谢您的帮助

我只想在id\u 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'];
    }
}

如果使用SQL获取此数据,则应该使用它的分组函数来完成此操作。