Recursive array merge, group by key when multidime

2019-08-17 06:42发布

问题:

I'm struggling with this one since i'm new with PHP. I'm crying for help to organise/tidy my output array.

I have an array like this (direct output from SQL, multiple joins).

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

And I want it to look like this :

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

As you can see : I want to iterate the first array and group cdatas by same season. After that, I want iterate each cdatas and group pdatas by ID.

Any help would be appreciated, i'm kind of lost with these multiple levels and keys.

Thank you guys !

For the moment, this is what I am doing, but with this code I have some season not grouped with the other, for a reason I don't explain.

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

}