Keys in PHP Array

2

I have a good question, I was able to identify the problem, but I could not solve it .. hahahaha

It is the following ... 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 in each array is not sequential, ie start at 0 and go to N. I am using to create the key the id of my records that are in the bank ... So far it's all right It's cute!

The problem is that when I use a FOREACH to go through the elements and display the values, as an example, "name" it returns me saying that INDEX does not exist ... however if I use an output vardump or print_r is displaying the values. I did a test running only once the foreach and it correctly displayed the NAME value the problem occurs after Key 2, I believe it is because it is not in the key order, however the problem is that the way I did I got access easier for the children of each Array ...

To get a better view I'm putting down the complete 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'm 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; ?>
    
asked by anonymous 03.01.2018 / 13:56

1 answer

2

From 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 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 an index to print, if it does not exist, it ignores and prints empty.

    
03.01.2018 / 14:12