Keys in PHP Array

Asked

Viewed 55 times

2

I have a very stupid doubt, I was able to identify the problem, but I could not solve it. hahahaha

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

The problem is that when I use a FOREACH to go through the elements and display the values, for example, "name" it returns me saying that the INDEX does not exist... but if I use a vardump output or print_r it is displaying the values. I did a test running only 1 time 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 order of keys, but the problem is that the way I did I can have 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; ?>
  • How you access the array in foreach?

  • @rray I added in the main theme as I am accessing the array in foreach..

  • 2

    You need to make another foreach in the key children

  • The foreach displays correctly the first time, but when using foreach again the problem occurs, would be this?

  • tries to do the following var_dump(array_column($menu , "name");, will return all names in an array

  • No, at the moment even in Children I am trying to access, I want to show first the values "Inicio", "Cadadastros", "Empresas" and etc.. when I try to access the name I get the return " Undefined index: name "

  • To access Children I know I will need to do another foreach worth $m['Children'], but the problem is being in the base value of the array...

Show 2 more comments

1 answer

2


By your comments on the question, you said:

when I try to access the name I get 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; ?>

This way, your code will check if there is an index to print, if it does not exist, it ignores and prints empty.

  • Putz David, what a mess of mine! Now that I went to see that there is not this, in case it was meant to be, It was a relationship error in the bank! Putz... Fight @David, save the trial!

Browser other questions tagged

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