1

I have an array as follows

Array ( [0] => application [1] => modules [2] => Ride [3] => externals [4] => images [5] => uploads [6] => profile [7] => 116 [8] => 13006678321287904362.jpg ) 

How can I insert an item to the existing array with out overwriting the existing element in specified index

Consider I would like to create as follows

Array ( [0] => application [1] => modules [2] => Ride [3] => externals [4] => images [5] => uploads [6] => profile [7] => 116 [8] => model [9] => 13006678321287904362.jpg ) 

Any help please

3 Answers 3

1

To elucidate on array_splice:

array_splice($array, $position, 0, $insert);

Where $array is the current array, $position is where you want to add the new item and $insert is the item to add (can be a new array)

Sign up to request clarification or add additional context in comments.

Comments

1

You can use array_slice, array_push and array_merge, I don't know if a better solution does exist.

1 Comment

I think you are right, he wants to insert new item in the middle of the array.
0

This should work for you if you want to add it to the end of the array

<?php $array[] = $var; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.