开发者

add values to array?

i've got an array with 开发者_Go百科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?


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


It's pretty simple, try something like this:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜