array_splice() function in php || PHP Array Splice Function
×


array_splice() function in php

2260

array_splice() function of PHP is used to remove one or more elements from a PHP array and add new elements from other PHP array. It is an updated version of array_slice() function. It is an inbuilt function of PHP.

Syntax:

array_splice($array,$start,$length,$other_array);

here,

  • $array is a PHP array on which we perform array_splice() function. It is mandatory.
  • $start represents the starting or initial point from where we want to remove one or more elements from an array. it is also mandatory.
  • $length is a number of elements that we want to remove from array. it is optional.
  • $other_array is a PHP array from which we add new elements into $array. it is also optional.

Example 1:

<?php
$arr1=array("FRUIT1"=>"apple","FRUIT2"=>"Mango","FRUIT3"=>"banana","FRUIT4"=>"pear");
$arr2=array("Guava","Fig");
array_splice($arr1,2,1,$arr2);
echo "<pre>";
print_r($arr1);
?>

Output:

Note: In the case of an associative array, the keys of new elements change into a numeric value.


Example 2:

<?php
$arr1=array("FRUIT1"=>"apple","FRUIT2"=>"Mango","FRUIT3"=>"banana","FRUIT4"=>"pear");
$arr2=array("FRUIT5"=>"Guava","FRUIT6"=>"Fig");  // associative array
array_splice($arr1,2,1,$arr2);
echo "<pre>";
print_r($arr1);
?>

Output:



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments