I'm trying to add the value to the array at a given index, but so far without any luck. I've got the following:

$array = array('first', 'second', 'third');
array_splice($array, 0, 0, array('another'));

which results in empty array.

I've also tried different offsets such as 1 or 2 - with the same result.

Could someone please explain what I'm doing wrong here?

有帮助吗?

解决方案

array_splice() modifies it's first argument by reference. The empty array is returns would contain the elements removed in the operation, if any were removed. Since you didn't remove any, it is empty. You original variable $array has been modified as expected.

$array = array('first', 'second', 'third');
array_splice($array, 0, 0, array('another'));
var_dump($array);

http://codepad.org/VI1WoW7M

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top