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

时间:2014-03-09 16:43:20

标签: php arrays recursion multidimensional-array iteration

因为我是PHP的新手,所以我正在努力解决这个问题。我正忙着帮忙整理/整理我的输出数组。

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

array(8) {
    [0] = > array(2) {
        ["season"] = > string(1)"1" 
        ["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"
                    }
                }
            }
        }
    }

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

    [2] = > 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"
                    }
                }
            }
        }
    }

    [3] = > array(2) {
        ["season"] = > string(1)"4" 
        ["cdatas"] = > array(1) {
            [0] = > array(2) {
                ["id"] = > string(2)"16"
                ["pdatas"] = > array(0) {}
            }
        }
    }
    [4] = > array(2) {
        ["season"] = > string(2)"11" 
        ["cdatas"] = > array(1) {
            [0] = > array(2) {
                ["id"] = > string(2)"16" 
                ["pdatas"] = > array(0) {}
            }
        }
    }
    [5] = > array(2) {
        ["season"] = > string(2)"11" 
        ["cdatas"] = > array(1) {
            [0] = > array(2) {
                ["id"] = > string(2)"17" 
                ["pdatas"] = > array(1) {
                    [0] = > array(2) {
                        ["name"] = > string(9)"Liga BBVA" 
                        ["res"] = > string(1)"1"
                    }
                }
            }
        }
    }
    [6] = > array(2) {
        ["season"] = > string(2)"11" 
        ["cdatas"] = > array(1) {
            [0] = > array(2) {
                ["id"] = > string(2)"17" 
                ["pdatas"] = > array(1) {
                    [0] = > array(2) {
                        ["name"] = > string(12)"Copa del Rey" 
                        ["res"] = > string(1)"2"
                    }
                }
            }
        }
    }
    [7] = > array(2) {
        ["season"] = > string(2)"11"
        ["cdatas"] = > array(1) {
            [0] = > array(2) {
                ["id"] = > string(2)"17" 
                ["pdatas"] = > array(1) {
                    [0] = > array(2) {
                        ["name"] = > string(21)"Supercoupe d\'Espagne" 
                        ["res"] = > string(1)"1"
                    }
                }
            }
        }
    }
} 

我希望它看起来像这样:

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"
                    }
                }
            }
        }
    }

正如您所看到的:我希望在同一季节迭代第一个数组并分组cdatas。 之后,我希望迭代每个cdatas并按ID分组pdatas。

任何帮助都会受到赞赏,我会因为这些多级别和关键而迷失方向。

谢谢你们!

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

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

}

0 个答案:

没有答案