This question already has an answer here:
这个问题已经有了答案:
- Insert new item in array on any position in PHP 16 answers
- 在PHP 16答案的任意位置插入数组中的新项
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.
我也尝试过不同的偏移量,比如1或2,结果是一样的。
Could someone please explain what I'm doing wrong here?
有人能解释一下我在这里做错了什么吗?
1 个解决方案
#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_splice()通过引用修改它的第一个参数。空数组is返回将包含在操作中删除的元素(如果有的话)。因为您没有删除任何内容,所以它是空的。原始变量$array已按预期进行修改。
$array = array('first', 'second', 'third');
array_splice($array, 0, 0, array('another'));
var_dump($array);
http://codepad.org/VI1WoW7M
#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_splice()通过引用修改它的第一个参数。空数组is返回将包含在操作中删除的元素(如果有的话)。因为您没有删除任何内容,所以它是空的。原始变量$array已按预期进行修改。
$array = array('first', 'second', 'third');
array_splice($array, 0, 0, array('another'));
var_dump($array);
http://codepad.org/VI1WoW7M