2

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?

0

1 Answer 1

6

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.