Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Recursion_Multidimensional Array_Iteration - Fatal编程技术网

Php 递归数组合并,多维时按键分组

Php 递归数组合并,多维时按键分组,php,arrays,recursion,multidimensional-array,iteration,Php,Arrays,Recursion,Multidimensional Array,Iteration,自从我是PHP新手以来,我一直在努力解决这个问题。我迫切需要帮助来组织/整理我的输出阵列 我有一个这样的数组(从SQL直接输出,多个连接) 我希望它看起来像这样: array(5) { [0] = > array(2) { ["season"] = > string(1)"1" ["cdatas"] = > array(1) { [0] = > array(2) { ["id

自从我是PHP新手以来,我一直在努力解决这个问题。我迫切需要帮助来组织/整理我的输出阵列

我有一个这样的数组(从SQL直接输出,多个连接)

我希望它看起来像这样:

array(5) {
    [0] = > array(2) {
        ["season"] = > string(1)"1" 
        ["cdatas"] = > array(1) {
            [0] = > array(2) {
                ["id"] = > string(1)"6" 
                ["pdatas"] = > array(2) {
                    [0] = > array(2) {
                        ["name"] = > string(7)"Ligue 1" 
                        ["res"] = > string(1)"1"
                    }
                    [1] = > array(2) {
                        ["name"] = > string(15)"Coupe de France" 
                        ["res"] = > string(1)"1"
                    }
                }
            }
        }
    }

    [1] = > array(2) {
        ["season"] = > string(1)"2" 
        ["cdatas"] = > array(1) {
            [0] = > array(2) {
                ["id"] = > string(1)"6" 
                ["pdatas"] = > array(1) {
                    [0] = > array(2) {
                        ["name"] = > string(7)"Ligue 1"
                        ["res"] = > string(1)"1"
                    }
                }
            }
        }
    }

    [2] = > array(2) {
        ["season"] = > string(1)"4" 
        ["cdatas"] = > array(1) {
            [0] = > array(2) {
                ["id"] = > string(2)"16"
                ["pdatas"] = > array(0) {}
            }
        }
    }
    [3] = > array(2) {
        ["season"] = > string(2)"11" 
        ["cdatas"] = > array(2) {
            [0] = > array(2) {
                ["id"] = > string(2)"16" 
                ["pdatas"] = > array(0) {}
            }
            [1] = > array(2) {
                ["id"] = > string(2)"17" 
                ["pdatas"] = > array(2) {
                    [0] = > array(2) {
                        ["name"] = > string(9)"Liga BBVA" 
                        ["res"] = > string(1)"1"
                    }
                    [1] = > array(2) {
                        ["name"] = > string(12)"Copa del Rey" 
                        ["res"] = > string(1)"2"
                    }
                    [2] = > array(2) {
                        ["name"] = > string(21)"Supercoupe d\'Espagne" 
                        ["res"] = > string(1)"1"
                    }
                }
            }
        }
    }
如您所见:我希望按同一季节迭代第一个数组和组CDATA。 之后,我希望通过ID迭代每个CDATA和组PDATA

任何帮助都将不胜感激,我对这些多重关卡和钥匙有点迷茫

谢谢你们

目前,这就是我正在做的,但是有了这段代码,我有一些季节没有与其他季节分组,原因我没有解释

function sortCollectiveSeasonList($arrayToSort) {

    $array = $arrayToSort;

    for ($i = 0; $i <= count($array); $i++) {

        for ($j = 0; $j <= count($array); $j++) {

            if ($array[$i]['season'] == $array[$j]['season'] && $i < $j) {

                //echo 'SAME SEASON FOUND season '.$array[$i][season].' and '.$array[$j][season].'';

                    if (isset($array[$i]['cdatas'])) {
                            $array[$i]['cdatas'] = array_merge($array[$i]['cdatas'], $array[$j]['cdatas']);
                    }


                    else {
                        $array[$i]['cdatas'] = $array[$j]['cdatas'];
                    }


                    unset($array[$j]);

            }

        }

        //if (isset($array[$i]['cdatas']))
            //$array[$i]['cdatas'] = sortList($array[$i]['cdatas']);

        //else
            //unset($array[$i]);

        //array_push($output, $array[$i]);

    }

    //Reindex array
    $array = array_values($array);


    //Remove empty cdatas
    for ($i = 0; $i < count($array); $i++) {

        if (isset($array[$i]['cdatas']))
            $array[$i]['cdatas'] = sortList($array[$i]['cdatas']);

        else
            unset($array[$i]);

    }


    //return $array;
    return array_values($array);
}


function sortList($arrayToSort) {

    $array = $arrayToSort;

    for ($i = 0; $i <= count($array); $i++) {

        for ($j = 0; $j <= count($array); $j++) {

            if ($array[$i]['id'] == $array[$j]['id'] && $i != $j) {

                if (isset($array[$i]['pdatas'])) {
                    $array[$i]['pdatas'] = array_merge($array[$i]['pdatas'], $array[$j]['pdatas']);
                }

                else {

                    $array[$i]['pdatas'] = $array[$j]['pdatas'];
                }

                unset($array[$j]);

            }

        }
    }

    //Reindex array
    $array = array_values($array);

    //Remove empty cdatas
    for ($i = 0; $i < count($array); $i++) {

        if (!isset($array[$i]['pdatas']))
            unset($array[$i]);

    }


    //return $array;
    return array_values($array);

}
函数sortCollectiveSeasonList($arrayToSort){
$array=$arrayToSort;

对于($i=0;$i),看起来您可以迭代第一个数组,并在执行此操作时创建一个新数组。在第一个级别(我认为是季节)已经有了匹配然后,您可以在同一块中插入CDATA和PDATA。不过,我们希望在这里首先看到初步的效果-您可以试一试,然后编辑您的成绩吗?谢谢您的回答。请参阅我的编辑,了解我目前使用的代码。由于我有季节(大部分是最后一个季节),结果与我的预期不完全一样未分组。我尝试使用for循环
function sortCollectiveSeasonList($arrayToSort) {

    $array = $arrayToSort;

    for ($i = 0; $i <= count($array); $i++) {

        for ($j = 0; $j <= count($array); $j++) {

            if ($array[$i]['season'] == $array[$j]['season'] && $i < $j) {

                //echo 'SAME SEASON FOUND season '.$array[$i][season].' and '.$array[$j][season].'';

                    if (isset($array[$i]['cdatas'])) {
                            $array[$i]['cdatas'] = array_merge($array[$i]['cdatas'], $array[$j]['cdatas']);
                    }


                    else {
                        $array[$i]['cdatas'] = $array[$j]['cdatas'];
                    }


                    unset($array[$j]);

            }

        }

        //if (isset($array[$i]['cdatas']))
            //$array[$i]['cdatas'] = sortList($array[$i]['cdatas']);

        //else
            //unset($array[$i]);

        //array_push($output, $array[$i]);

    }

    //Reindex array
    $array = array_values($array);


    //Remove empty cdatas
    for ($i = 0; $i < count($array); $i++) {

        if (isset($array[$i]['cdatas']))
            $array[$i]['cdatas'] = sortList($array[$i]['cdatas']);

        else
            unset($array[$i]);

    }


    //return $array;
    return array_values($array);
}


function sortList($arrayToSort) {

    $array = $arrayToSort;

    for ($i = 0; $i <= count($array); $i++) {

        for ($j = 0; $j <= count($array); $j++) {

            if ($array[$i]['id'] == $array[$j]['id'] && $i != $j) {

                if (isset($array[$i]['pdatas'])) {
                    $array[$i]['pdatas'] = array_merge($array[$i]['pdatas'], $array[$j]['pdatas']);
                }

                else {

                    $array[$i]['pdatas'] = $array[$j]['pdatas'];
                }

                unset($array[$j]);

            }

        }
    }

    //Reindex array
    $array = array_values($array);

    //Remove empty cdatas
    for ($i = 0; $i < count($array); $i++) {

        if (!isset($array[$i]['pdatas']))
            unset($array[$i]);

    }


    //return $array;
    return array_values($array);

}