Keys in Array PHP



  • I'm having a very silly doubt, I was able to identify the problem, but I couldn't solve it. hahahahahahahahahahahahahahahahaha

    Here's the thing. I have the following array:

    array:7 [▼
      1 => array:5 [▶]
      2 => array:5 [▶]
      4 => array:5 [▶]
      10 => array:5 [▶]
      16 => array:5 [▶]
      22 => array:5 [▶]
      13 => array:1 [▶]
    ]
    

    You may notice that my key to each array is not sequential, i.e., start at 0 and go to N. I'm using to create the id key of my records that are in the bank... So far everything is right and beautiful!

    The problem is that when I use a FOREACH to go through the elements and display the values, as example, "name" it returns to me talking that the INDEX does not exist... but if I use a vardump or print_r output is displaying the values. I did a test by only 1 time foreach and he displayed correctly the value NAME the problem occurs after Key 2, I believe it is because it is not in the order of keys, but the problem is that the way I did I can get easier access to the children of each Array...

    To get a better view I'm putting down the full Array:

    Array
    (
        [1] => Array
            (
                [id] => 1
                [name] => Inicio
                [link] => #start
                [icon] => fa fa-tachometer
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 8
                                [name] => Dashboard Eventos
                                [link] => index/Dashboard-Vendas
                                [icon] => fa fa-caret-right
                            )
    
                    [1] => Array
                        (
                            [id] => 9
                            [name] => Dashboard Monitor
                            [link] => index/Dashboard-Monitor
                            [icon] => fa fa-caret-right
                        )
    
                )
    
        )
    
    [2] => Array
        (
            [id] => 2
            [name] => Cadastros
            [link] => #clientes
            [icon] => fa fa-user
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => Clientes
                            [link] => clientes/Clientes
                            [icon] => fa fa-caret-right
                        )
    
                )
    
        )
    
    [4] => Array
        (
            [id] => 4
            [name] => Empresa
            [link] => #empresas
            [icon] => fa fa-building
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [name] => Usuarios
                            [link] => empresas/Colaborador
                            [icon] => fa fa-caret-right
                        )
    
                    [1] => Array
                        (
                            [id] => 11
                            [name] => Equipes
                            [link] => empresas/Equipes
                            [icon] => fa fa-caret-right
                        )
    
                    [2] => Array
                        (
                            [id] => 12
                            [name] => Minhas Empresas
                            [link] => empresas/Empresas
                            [icon] => fa fa-caret-right
                        )
    
                    [3] => Array
                        (
                            [id] => 20
                            [name] => Veiculos Valores
                            [link] => empresas/Veiculos-Valor
                            [icon] => fa fa-caret-right
                        )
    
                    [4] => Array
                        (
                            [id] => 25
                            [name] => Departamentos
                            [link] => empresas/Departamentos
                            [icon] => fa fa-caret-right
                        )
    
                )
    
        )
    
    [10] => Array
        (
            [id] => 10
            [name] => Oportunidades
            [link] => #oportunidades
            [icon] => fa fa-thumbs-up
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 7
                            [name] => Relatorios
                            [link] => oportunidades/Oportunidades
                            [icon] => fa fa-caret-right
                        )
    
                    [1] => Array
                        (
                            [id] => 21
                            [name] => Carregar Eventos
                            [link] => oportunidades/Upload-Eventos
                            [icon] => fa fa-caret-right
                        )
    
                )
    
        )
    
    [16] => Array
        (
            [id] => 16
            [name] => Mesa de Credito
            [link] => #credito
            [icon] => fa fa-money
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 17
                            [name] => Propostas
                            [link] => propostas/Propostas
                            [icon] => fa fa-caret-right
                        )
    
                    [1] => Array
                        (
                            [id] => 18
                            [name] => Relatorios
                            [link] => credito/Relatorios
                            [icon] => fa fa-caret-right
                        )
    
                )
    
        )
    
    [22] => Array
        (
            [id] => 22
            [name] => Pesquisa Satisfacao
            [link] => #pesquisas
            [icon] => fa fa-line-chart
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 23
                            [name] => Pesquisas
                            [link] => pesquisas/Pesquisas
                            [icon] => fa fa-caret-right
                        )
    
                    [1] => Array
                        (
                            [id] => 24
                            [name] => Relatorios
                            [link] => pesquisas/Relatorios
                            [icon] => fa fa-caret-right
                        )
    
                )
    
        )
    
    [13] => Array
        (
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 14
                            [name] => Minhas Campanhas
                            [link] => campanhas/Minhas-Campanhas
                            [icon] => fa fa-caret-right
                        )
    
                    [1] => Array
                        (
                            [id] => 15
                            [name] => Relatorios
                            [link] => campanhas/Relatorios
                            [icon] => fa fa-caret-right
                        )
    
                )
    
        )
    

    )

    I am accessing the Array as follows:

    <?php foreach($menu as $m): ?>
    <a class="nav-item nav-link" data-toggle="tab" href="#nav-file"><?php $m['name']; ?></a>
    <?php endforeach; ?>



  • By your comments on the question, you said:

    when I try to access the name I have the return " Undefined index: name

    This is because the array in the position [13] does not have the index name. To fix this, you can put your foreach as follows:

    <?php foreach($menu as $m): ?>                  
        <a class="nav-item nav-link" data-toggle="tab" href="#nav-file"><?php echo (isset($m['name'])) ? $m['name'] : ''; ?></a>                                 
    <?php endforeach; ?>
    

    In this way, your code will check if there is the index to print, if there is no, it ignores and prints empty.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2