tags:

views:

55

answers:

3

i've got an array with existing key/value-pair and i want to add values to the keys after the existing ones without deleting anything.

how do i do that?

+1  A: 

It's pretty simple, try something like this:

$new_array = array('blah' => 'blah');
array_push($existing_array, $new_array);
Chuck Vose
+2  A: 
$values["names"] = "jonathan";

I could add various other values to that like this:

$values["names"] = array($values["names"], "sara", "rebecca");

You can also add values like this:

$values["names"][] = "Jonathan";
$values["names"][] = "Sara";
$values["names"][] = "Rebecca";

I'm assuming this is what you meant.

Jonathan Sampson
$values["names"] = array($values["names"], "sara", "rebecca"); <-- This wouldn't work, it would create a multidimensional array in $values["names"]. You could do$values['names'] = array_merge($values['names'], array('sara', 'rebecca'));
LM
I know it would create a multidimensional array, LM. That was the intended purpose :)
Jonathan Sampson
A: 

Keep in mind that an array in PHP is not an array, it is a pairwise associative container. When you say "after" it depends on what sort of indexing you're doing. If you have numerical indices, you can use the $foo[] = bar notation to get the next numerical index. If there are no numerical indices, it will start at 0. If you want to check that the index doesn't already exist when you're inserting something, you can always use array_key_exists($key, $array).

Alex Kaminoff