Access information in Multidimensional Associative array (PHP)?

Asked

Viewed 864 times

2

I have the following structure:

$arrDados = Array ( 
    [0] => Array ( 
        [NumeroEmpenhoNumero] => Array ( 
            [type] => NULL 
            [size] => 0 
            [deci] => 0 
            [code] => 0 
        ) 
    ) 
    [1] => Array ( 
        [NumeroEmpenhoAno] => Array ( 
            [type] => NULL 
            [size] => 0 
            [deci] => 0 
            [code] => 0 
        ) 
    ) 
    [2] => Array ( 
        [UnidadeOrcamentaria] => Array ( 
            [type] => NULL 
            [size] => 0 
            [deci] => 0 
            [code] => 0 
        ) 
    ) 
    [3] => Array ( 
        [UnidadeExecutora] => Array ( 
            [type] => NULL 
            [size] => 0 
            [deci] => 0 
            [code] => 0 
        ) 
    ) 
    [4] => Array ( 
        [Historico] => Array ( 
            [type] => NULL 
            [size] => 0 
            [deci] => 0 
            [code] => 0 
        ) 
    ) 
    [5] => Array ( 
        [OperacaoCadastro] => Array ( 
            [type] => NULL 
            [size] => 0 
            [deci] => 0 
            [code] => 0
        ) 
    )
);

I need to access something as:

$arrDados['NumeroEmpenhoNumero']['code'] 

or

$arrDados['NumeroEmpenhoNumero']['size']

but keeps giving error:

Undefined offset: 'Numeroempenhonumero' in ...on line 222

  • How can I access these positions of this array?
  • What is the correct syntax?

1 answer

2

You are forgetting the index of the array, you can access it as follows:

echo  $arrDados[0]["NumeroEmpenhoNumero"]["size"]; // Acessando de forma fixa


foreach ($arrDados as $key => $value) { 
   foreach ($value as $k => $v) {
      echo $v["size"]; //Acessando de forma dinâmica

   }
}
  • Thanks, it worked out.

Browser other questions tagged

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