Level PHP Array

-1

I need to put this array all on the same level, I tried that way, but I could not leave everyone at the same level, below the input, output and code I tried.

Entry

[
    (int) 0 => [
        'id' => (int) 1,
        'nome' => 'Administrador'
    ],
    (int) 1 => [
        (int) 0 => [
            'id' => (int) 17,
            'nome' => 'Administrador > Revenda 1'
        ],
        (int) 1 => [
            (int) 0 => [
                'id' => (int) 20,
                'nome' => 'Administrador > Revenda 1 > Revenda Teste > 1'
            ],
            (int) 1 => [
                (int) 0 => [
                    'id' => (int) 25,
                    'nome' => 'Administrador > Revenda 1 > Revenda Teste > 1 > Revenda Teste 2 - 1'
                ]
            ],
            (int) 2 => [
                'id' => (int) 21,
                'nome' => 'Administrador > Revenda 1 > Reventa Teste > 2'
            ],
            (int) 3 => [
                'id' => (int) 22,
                'nome' => 'Administrador > Revenda 1 > Revenda Teste  > 3'
            ]
        ]
    ]
]

I need this output:

[
(int) 0 => [
    'id' => (int) 1,
    'nome' => 'Administrador'
],
(int) 1 => [
    'id' => (int) 17,
    'nome' => 'Administrador > Revenda 1'
],
(int) 2 => [
    'id' => (int) 20,
    'nome' => 'Administrador > Revenda 1 > Revenda Teste > 1'
],
(int) 3 => [
    'id' => (int) 25,
    'nome' => 'Administrador > Revenda 1 > Revenda Teste > 1 > Revenda Teste 2 - 1'
],
(int) 4 => [
    'id' => (int) 21,
    'nome' => 'Administrador > Revenda 1 > Reventa Teste > 2'
],
(int) 5 => [
    'id' => (int) 22,
    'nome' => 'Administrador > Revenda 1 > Revenda Teste  > 3'
]
]

My code:

    public function organizarMenu($menu){

    foreach($menu as $key => $val){
        if(isset($val["id"])){
            $newmenu[] = ['id' => $val['id'], 'nome' => $val['nome']];
        }else{
            $newmenu[] = $this->organizarMenu($val);
        }

    }
    return $newmenu;
}
    
asked by anonymous 13.04.2018 / 16:10

1 answer

3

This recursive function will solve your problem:

Since $array_base is your initial array presented and $array_final is the desired output.

<?php
    $array_base = array(
        0=>array('id'=>1, 'nome'=>'Administrador'),
        1=>array(
            0=>array('id'=>17, 'nome'=>'Administrador > Revenda 1'),
            1=>array(
                0=>array('id'=>20, 'nome'=>'Administrador > Revenda 1 > Revenda Teste > 1'),
                1=>array(
                    0=>array('id'=>25, 'nome'=>'Administrador > Revenda 1 > Revenda Teste > 1 > Revenda Teste 2 - 1'),
                ),
                2=>array('id'=>20, 'nome'=>'Administrador > Revenda 1 > Revenda Teste > 2'),
                3=>array('id'=>20, 'nome'=>'Administrador > Revenda 1 > Revenda Teste > 3'),
            ),
        ),
    );

    $array_final = array();

    foreach ($array_base as $resultado) {
        if(isset($resultado['id'])){
            $array_final[]= $resultado;
        }else{
            $array_final = subnivel($resultado, $array_final);
        }
    }

    var_dump($array_final);exit;

    function subnivel($array_subnivel, $array_final){
        foreach ($array_subnivel as $subnivel) {
            if(isset($subnivel['id'])){
                $array_final[]= $subnivel;
            }else{
                $array_final = subnivel($subnivel, $array_final);
            }
        }
        return $array_final;
    }
?>

The var_dump will return a one-dimensional array of size 6, with each array inside it containing id and nome .

You can still see the code working with the output you want at: www.ideone.com/cjNrRF

    
13.04.2018 / 16:35