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

6
  • 1
    $existingArray = []; $existingArray['mynewkey'] = 'mynewvalue';? Commented May 20, 2016 at 10:25
  • 1
    using foreach and just do like: $value[$key] = $new_val Commented May 20, 2016 at 10:25
  • @Frayne, Can you show an example with the 'foreach' pls.. Commented May 20, 2016 at 10:31
  • yes check my answer. Commented May 20, 2016 at 10:34
  • 1
    what if the key & value would be dynamic? Commented May 20, 2016 at 10:40

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

4 Comments

Just bear in mind with this that, after the loop, setting $val again will modify the last element in $arr - so watch out!
@halfer, So is there any ways to dismiss this?? I think unset($val);
Absolutely yes, that destroys the write-back link. I wrote about this here.
isnt there a predefined php function for that? i was hoping for one
4
foreach($arr as $key => $row){
  $arr[$key]['color']="red";
}

3 Comments

or you can use example from @frayne-konok, but you don't need $key =>, just write foreach ($arr as &$val)
yeah peer that helped me, but can you explain me whats the purpose for '&$'?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.