Level PHP Array

Asked

Viewed 106 times

-1

I need to put this array all on the same level, I tried that way, but I couldn’t leave everyone on the same level, it goes down the input, output and the code I tried.

Entree

[
    (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 exit:

[
(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;
}
  • 1

    "but something is missing" what’s missing?

  • Missing the way out.

  • I understood the question. See my answer if you answer.

1 answer

3


This recursive function will solve your problem:

whereas $array_base is its incial array presented and $array_final is the desired exit.

<?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 tamano 6, with each array within it containing id and nome.

You can still see the code working with the desired output in: www.ideone.com/cjNrRF

  • 1

    You can still see the code working with the desired output at: https://ideone.com/cjNrRF

  • Perfect man!!! Exactly that.

  • Thank you! @Davidalves

  • Good thing it worked ;D

  • Put the Ideone link in the answer that makes the most sense, and help anyone who wants to test the code you propose

  • Boa @Isac edited the answer.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.