Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

push to specific position of array [duplicate]

Tags:

arrays

php

Possible Duplicate:
Insert into array at a specified place

How to push 1 or more values to middle (or a specific position/index) of an array? for example:

$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');
like image 775
mrdaliri Avatar asked Feb 15 '12 15:02

mrdaliri


1 Answers

array_splice is probably what you're looking for:

array_splice($a, 1, 0, array('c', 'd'));
like image 119
KARASZI István Avatar answered Oct 15 '22 00:10

KARASZI István