Domanda

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?

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top