0

I have $monTab, an array with nested arrays like this in php :

array (size=12)
  0 => 
    array (size=2)
      'mon' => string '2018-01-01 00:00:00' (length=19)
      'nb_argus' => string '29' (length=2)
  1 => 
    array (size=2)
      'mon' => string '2018-02-01 00:00:00' (length=19)
      'nb_argus' => string '21' (length=2)
  2 => 

I am simply trying to add this new pair of key value to each of the nested arrays :

'tx'  => int '50' (length=2)

So i've built a for each like that :

foreach($monTab as $item) {
      $item["tx"] = 50;
}

It doesnt work at all, var_dump($monTab) shows that nothing has happened !

the tx key is not added at all, the value is not added at all to my arrays !!

jaja5000
  • 51
  • 8

1 Answers1

1

Due to the side effect of using pass by reference with foreach(...), using array_walk() or array_map() may be an idea.

array_walk($monTab, function(&$m){
    $m['tx'] = 50; 
});
jj40308
  • 21
  • 5