Access information in Multidimensional Associative Array (PHP)? [duplicate]

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 like :

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

or

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

But you're giving error :

  

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

  • How do I access these positions from this array?
  • What is the correct syntax?
asked by anonymous 21.05.2017 / 03:07

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

   }
}
    
22.05.2017 / 17:57