开发者

PHP - write to first element of an array

How to write to the first element of an array?

I know reset can return the first element... but 开发者_JAVA技巧you can not use it to write to it.


Anything wrong with $yourarray[0] = $value ?

If you don't want to overwrite the first element, try "array_unshift":

http://www.php.net/manual/en/function.array-unshift.php

EDIT: ok, use this for non-numerical keys:

reset($yourarray);
$key = key($yourarray);
$yourarray[$key] = $newvalue;


That's called an "associative array" or a "hash". Technically, it doesn't have an order. You may have an item that you've put in first, but that's only incidental.


does this work?

reset($x);
$x[0] = $value;


Do you mean prepend the array with a value?

array_unshift() - this is costly, rebuilding the array.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜