1). <?
php
//Original Array on which operations is to be perform
$original_array = array( '1', '2', '3', '4', '5' );
echo 'Original array : ';
foreach ($original_array as $x)
echo "$x ";
echo "\n";
//value of new item
$inserted_value = '11';
//value of position at which insertion is to be done
$position = 2;
//array_splice() function
array_splice( $original_array, $position, 0, $inserted_value );
echo "After inserting 11 in the array is : ";
foreach ($original_array as $x)
echo "$x ";
?>
Output
Original array : 1 2 3 4 5
After inserting 11 in the array is : 1 2 11 3 4 5
2) <?php
$arr1 = array("Geeks", "g4g");
$arr2 = array("GeeksforGeeks", "Computer science portal");
// Get the merged array in the first array itself.
$arr1 = array_merge($arr1, $arr2);
echo "arr1 Contents:";
// Use for each loop to print all the array elements.
foreach ($arr1 as $value) {
echo $value . "\n";
?>
3). <?php
$arr1 = array(1, 2);
$arr2 = array(3, 4);
// arr2 elements are being pushed in the arr1.
array_push($arr1 , ...$arr2);
echo "arr1 = ";
// Use for each loop to print all the array elements.
foreach ($arr1 as $value) {
echo $value . ' ';
?>
<?php
// Declaring an associative array
$ass_arr = ["a" => "Geeks", "b" => "For", "c" => "Geeks"];
// Finding and deleting the element with value "For"
$key = array_search("For", $ass_arr);
if ($key !== false) {
unset($ass_arr[$key]);
}
// Printing array after deleting the element
print_r($ass_arr);
// Declaring an indexed array
$ind_arr = ["Geeks", "For", "Geeks"];
// Finding and deleting the element with value "For"
$index = array_search("For", $ind_arr);
if ($index !== false) {
unset($ind_arr[$index]);
// Printing array after deleting the element
print_r($ind_arr);
?>
<?php
// Declare an array
$array = array(
"GeeksforGeeks",
"Computer",
"Science",
"Portal"
);
// Declare a variable containing element
$element = "Welcome";
// User array_unshift() function to
// insert element at beginning of array
array_unshift( $array, $element );
print_r($array);
?>