0

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
  • would you still want them to be strictly one dimensional? Commented Dec 4, 2009 at 1:37

3 Answers 3

3
$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.

Sign up to request clarification or add additional context in comments.

2 Comments

$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'));
I know it would create a multidimensional array, LM. That was the intended purpose :)
1

It's pretty simple, try something like this:

$new_array = array('blah' => 'blah');
array_push($existing_array, $new_array);

Comments

0

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).

Comments

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.