Array_push in multidimensional associative arrays in php

0

I started an array ($ arrDados = array ();) and now I need to add information (array_push) so that I can access the information later as follows:

colA1 = $arrDados['NumeroEmpenhoAno']['code'];
colA2 = $arrDados['NumeroEmpenhoAno']['size'];

colB1 = $arrDados['UnidadeOrc']['code'];
colB2 = $arrDados['UnidadeOrc']['size'];

What would be the array_push code for accessing this array in the above structure?

I'm trying the code below, but it's wrong:

$arrDados=array();
array_push($arrDados,array('NumeroEmpenhoAno' => array('code' => 0,'size' => 1)));
array_push($arrDados,array('UnidadeOrc' => array('code' => 1,'size' => 2)));

The way I'm doing, I'm having to access it as follows:

colA1 = $arrDados[0]['NumeroEmpenhoAno']['code'];

But I want to:

colA1 = $arrDados['NumeroEmpenhoAno']['code'];

That is, without indexes.

    
asked by anonymous 21.05.2017 / 03:48

1 answer

1

You do not need array_push , you can just do:

$arrDados = array();

$arrDados += array('NumeroEmpenhoAno' => array('code' => 0,'size' => 1));
$arrDados += array('UnidadeOrc' => array('code' => 1,'size' => 2));

However one of the most common ways to do this:

$arrDados = array();

$arrDados['NumeroEmpenhoAno'] = array('code' => 0,'size' => 1);
$arrDados['UnidadeOrc'] = array('code' => 1,'size' => 2);

Both can be accessed by $arrDados['UnidadeOrc']['code'] , without the use of [1] .

Try this.

    
21.05.2017 / 04:29