Q:

Write a PHP script that inserts a new item in an array in any position

0

Write a PHP script that inserts a new item in an array in any position

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
$original = array( '1','2','3','4','5' );
echo 'Original array : '."\n";
foreach ($original as $x) 
{echo "$x ";}
$inserted = '$';
array_splice( $original, 3, 0, $inserted ); 
echo " \n After inserting '$' the array is : "."\n";
foreach ($original as $x) 
{echo "$x ";}
echo "\n"
?>

Sample Output:

Original array :                                            
1 2 3 4 5                                                   
 After inserting '$' the array is :                         
1 2 3 $ 4 5

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now