PHP Array Exercises : Inserts a new item in an array in any position
7. Insert New Array Item by Position
Write a PHP script that inserts a new item in an array in any position.
Sample Solution:
PHP Code:
Output:
Original array : 1 2 3 4 5 After inserting '$' the array is : 1 2 3 $ 4 5
Flowchart:

For more Practice: Solve these Related Problems:
- Write a PHP script to insert an element into the middle of an indexed array and then print the modified array.
- Write a PHP function to insert a given value at a specified index in an array and return the updated array.
- Write a PHP program to add an element into an array without overwriting any current elements, using array_splice().
- Write a PHP script to insert multiple new items at different positions in an array and then display the final array.
Go to:
PREV : Decode JSON String.
NEXT : Sort Associative Array by Key and Value.
PHP Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.