7

I do not know how to add a key and value to the existing array. My array goes like this. Initially I have tried adding using array_push() but it added not as I needed it.

I have given my output after I gave the 'var_dump'.

array (size=5)
  0 => 
    array (size=3)
     'id' => int 7
      'title' => string 'Pongal' (length=6)
      'start' => string '2016-05-16' (length=10)
  1 => 
    array (size=3)
       'id' => int 8
      'title' => string 'big day' (length=7)
      'start' => string '2016-05-04' (length=10)
  2 => 
    array (size=3)
      'id' => int 9
      'title' => string 'marriage day' (length=12)
      'start' => string '2016-05-19' (length=10)
  3 => 
    array (size=3)
      'id' => int 10
      'title' => string 'Karthiks  bday' (length=14)
      'start' => string '2016-06-11' (length=10)
  4 => 
    array (size=3)
      'id' => int 12
     'title' => string 'Election date announced' (length=23)
      'start' => string '2016-06-01' (length=10)

Now, I'd like to insert array('sample_key' => 'sample_value') after all the elements of each array.

How can I do it? This is I want the result to be like this:-

array (size=5)
  0 => 
    array (size=4)
      'id' => int 7
      'title' => string 'Pongal' (length=6)
      'start' => string '2016-05-16' (length=10)
      ‘color’ => ‘red’
  1 => 
    array (size=4)
      'id' => int 8
      'title' => string 'big day' (length=7)
      'start' => string '2016-05-04' (length=10)
      ‘color’ => ‘red’
  2 => 
    array (size=4)
      'id' => int 9
      'title' => string 'marriage day' (length=12)
      'start' => string '2016-05-19' (length=10)
      ‘color’ => ‘red’
  3 => 
    array (size=4)
      'id' => int 10
      'title' => string 'Karthiks  bday' (length=14)
      'start' => string '2016-06-11' (length=10)
      ‘color’ => ‘red’
  4 => 
    array (size=4)
      'id' => int 12
      'title' => string 'Election date announced' (length=23)
      'start' => string '2016-06-01' (length=10)
      ‘color’ => ‘red’

Note that I have added 'color' => 'red' to all the indexes

bcag2
  • 1,988
  • 1
  • 17
  • 31
Deepak Keynes
  • 2,291
  • 5
  • 27
  • 56

2 Answers2

15

Just do this: Working demo

using the & you can change the main array, and just use $val['color'] = 'red' to add a new key , value pair in the array.

foreach($arr as $key => &$val){
    $val['color'] = 'red';
}

Note that the 'write-back' feature of the ampersand persists even after the loop has finished: resetting $val to a new value will change the last element in $val, which is often unexpected. There are three ways around this class of bug:

  • Avoid write-back and just use the full array expression to write values inside the loop;
  • Don't re-use the $val variable in the same scope, even for another foreach() loop;
  • Use unset() on the $val variable to disconnect it from the array it will write back to.
halfer
  • 19,824
  • 17
  • 99
  • 186
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
  • Just bear in mind with this that, after the loop, setting `$val` again will modify the last element in `$arr` - so watch out! – halfer May 20 '16 at 11:22
  • @halfer, So is there any ways to dismiss this?? I think `unset($val);` – Murad Hasan May 20 '16 at 11:25
  • Absolutely yes, that destroys the write-back link. I [wrote about this here](https://blog.jondh.me.uk/2011/05/seriously-fiendish-php-gotcha/). – halfer May 20 '16 at 12:35
  • isnt there a predefined php function for that? i was hoping for one – Dimitar Jun 12 '19 at 10:50
4
foreach($arr as $key => $row){
  $arr[$key]['color']="red";
}
newbie67
  • 41
  • 2
  • or you can use example from @frayne-konok, but you don't need `$key =>`, just write `foreach ($arr as &$val)` – newbie67 May 20 '16 at 10:40
  • yeah peer that helped me, but can you explain me whats the purpose for '&$'? – Deepak Keynes May 20 '16 at 10:47
  • 1
    I can try, but sorry for my english. When you use `foreach($array as $key=>$value)`, you have new variable $value everytime, which is **equal** to `$array[$key]`. But when you use `&$`, `$value` **not equal** to `$array[$key]`, it's **the same** – newbie67 May 20 '16 at 10:54