Populate array dynamically

0

Well, I need your help. I have the following fixed structure in php:

'labels' => array('Jan','Fev','Mar')......

But I want to populate these internal values of the array dynamically, ie I'm going to get the bank some random months and use it in there. But I try to do this:

$meu_array = array();
// preencho o array

'labels' => array($meu_array);

But it does not work. How do I do this?

    
asked by anonymous 27.02.2018 / 13:09

3 answers

0

If you make the correct array with the data coming from the bank, it is to function the same as your months example. I made an example here or so.

$a = array('labels' => array('jav', 'fev', 'marc'));
$my_array = array('jav', 'fev', 'marc');
$new_array = array('labels' => $my_array);
  

It will look like this:

Array
(
    [labels] => Array
    (
        [0] => jav
        [1] => fev
        [2] => marc
        )

    )

segundo array 
Array
(
    [labels] => Array
    (
        [0] => jav
        [1] => fev
        [2] => marc
        )

    )

Look exactly like it. If you are not able to access your second array in the indexes it is because it is not populating properly.

Always check that the array is getting var_dump, print_r if it is not able to access content.

    
27.02.2018 / 13:21
0

You can create a dynamic array using "[]":

$array['labels'][] = 'Jan';
$array['labels'][] = 'Mar';
$array['labels'][] = 'Fev';

var_dump($array);

result:

array (size=1)
  'labels' => 
     array (size=3)
       0 => string 'Jan' (length=3)
       1 => string 'Mar' (length=3)
       2 => string 'Fev' (length=3)
    
27.02.2018 / 13:44
0

When I fill array dynamically I do in array form, which transfer the table from the bank directly to array:

$new_table = array();

  for($a = 0; $a < 2; $a++){
  //Avança as linhas
  $new_table[$a][$b] = $varbanco['Meses'];
  $new_table[$a][$b+1] = $varbanco['Ano'];
  $new_table[$a][$b+2] = $varbanco['Dia'];
}

Of course there should be a simpler way, but this way you can add future information if necessary

    
27.02.2018 / 14:29