Rearrange array with parents and children

-2

I'm doing a select in my database by skipping a lot of information, and I'm saving the results in an array, which is organized this way: link

What I'm trying to do is sort the parents and children according to the level shown in the image, placing the children inside a new array (called children ) of their respective parent.

For example ( link ):

0 => {
    "ordination": "10501"
    "level": 3
    "children":
    0 => {
        "ordination": "1050101"
        "level": 4
        "children":
            0 => {
                "ordination": "1050101001"
                "level": 5
            }
            1 => {
                "ordination": "1050101002"
                "level": 5 
            }
    1 => {
        "ordination": "1050102"
        "level": 4
        "children":
        0 => {
            "ordination": "1050101001"
            "level": 5
        }
    }
}

I tried to think of a logic running through the array backwards, but it does not work when there are several level 5 elements, for example, so I could not finish.

I would like help with some logic for me to understand and program. Thanks!

    
asked by anonymous 03.11.2018 / 20:57

1 answer

0

First you have to bring the data sorted from the lowest level to the highest level and then add the levels that are the same.

function groupLevels($todosLevels){
   $levelsGroup = [];
   foreach($todosLevels as $l){
      $levelsGroup[$l['level']]['level'] = $l['level'];
      $levelsGroup[$l['level']]['levels'][] = $l;
   }
   return $levelsGroup;
}

Then create a recursive function to look for your children.

    function getFilho($level,$todosLevels){

    $aFilho = [];
    foreach($todosLevels as $l){
         if($l['level'] > $level){
             $aFilho = $l['levels'];
             $Filhos = getFilho($l['level'],$todosLevels);
             if($Filhos){
                 $aFilho['children'] = $Filhos;
             }
             break;
         }
    }
    return $aFilho;

}

It would look like this:

    <?php

        $aLevels = [
        [
             'ordination' => 10501,
             'level' => 1,
        ],
        [
             'ordination' => 10502,
             'level' => 2,
        ],
        [
             'ordination' => 10512,
             'level' => 2,
        ],
        [
             'ordination' => 10503,
             'level' => 3,
        ],
        [
             'ordination' => 10504,
             'level' => 4
        ],
    ];

$aGroupLevels = groupLevels($aLevels);


/* iniciando primeiro level */
$aLevelsAndChildrens = [];
$aLevelsAndChildrens[0] = $aLevels[0];

$aFilhos = getFilho($aLevels[0]['level'],$aGroupLevels);

if($aFilhos){
    $aLevelsAndChildrens[0]['children'] = $aFilhos;
}

print_r($aLevelsAndChildrens);

Array
(
    [0] => Array
        (
            [ordination] => 10501
            [level] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [ordination] => 10502
                            [level] => 2
                        )

                    [1] => Array
                        (
                            [ordination] => 10512
                            [level] => 2
                        )

                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [ordination] => 10503
                                    [level] => 3
                                )

                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [ordination] => 10504
                                            [level] => 4
                                        )

                                )

                        )

                )

        )

)
    
04.11.2018 / 18:54