arrays - How to insert an element in any position in php -


I am with an array like $ x = array (1,2,3,4,5); I would like to add element 6 between 3 and 4 and it will be array (1,2,3,6,4,5);

Would you like to make it in that place or in the first place?

Try it out:

  $ x = array (1,2) , 3,4,5); $ X = array_merge (array_slice ($ x, 0, 3), array (6), array_slice ($ x, 3)); Print_r ($ x);  

output;

  array ([0] => 1 [1] => 2 [2] => 3 [3] = & gt; 6 [4] = & gt; ; 4 [5] = & gt; 5)  

Comments