fayer
fayer

Reputation: 17

add values to array?

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?

Upvotes: 0

Views: 345

Answers (3)

Alex Kaminoff
Alex Kaminoff

Reputation: 79

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

Upvotes: 0

Sampson
Sampson

Reputation: 268334

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

Upvotes: 3

Chuck Vose
Chuck Vose

Reputation: 4580

It's pretty simple, try something like this:

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

Upvotes: 1

Related Questions