将元素推送到数组的特定位置

5

可能是重复问题:
在指定位置插入数组

如何将1个或多个值推到数组的中间(或特定位置/索引)? 例如:

$a = array('a', 'b', 'e', 'f');
array_pushTo($a, 1, 'c', 'd'); // that function i'm looking for. first parameter is the array, second is the index, and third and other are the values.
// $a now is: array('a', 'b', 'c', 'd', 'e', 'f');

array_splice() http://us3.php.net/manual/zh/function.array-splice.php - Michael Berkowski
这应该会对你有所帮助 :)https://dev59.com/b2865IYBdhLWcg3wd-ce - user400055
下面有一个答案需要被接受。 - Richard Hedges
1
@RichardHedges:哇!谢谢,经过2年多,当我完全忘记它时,我标记了最佳答案。 - mrdaliri
1个回答

25

array_splice 是你可能正在寻找的函数:

array_splice($a, 1, 0, array('c', 'd'));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接