PHP array_splice() Function

In this chapter you will learn:

  1. Definition for PHP array_splice() Function
  2. Syntax for PHP array_splice() Function
  3. Parameter for PHP array_splice() Function
  4. Note for PHP array_splice() Function
  5. Example - Replace elements in an array
  6. Example - With the length parameter set to 0
  7. Example - inserts two new elements at the third position in the array
  8. Example - remove and insert elements at the same time
  9. Example - What happens if you omit the third argument
  10. Example - array_splice() automatically casts the fourth argument to an array before using it

Definition

array_splice() function is the array equivalent of the string - manipulation function substr_replace().

The array_splice() function removes a range of elements and replaces them with new elements.

Both the removal and the replacement are optional, meaning you can just remove elements without adding new ones, or just insert new elements without removing any.

The function returns an array with the removed elements.

Syntax

PHP array_splice() Function has the following syntax.

array_splice(array,start,length,array)

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to splice
startRequired.start position. 0 = the first element. A negative number means to start from the last element. -2 means start at the second last element of the array.
lengthOptional.how many elements to remove, and length of the returned array. For a negative number, the function will stop that far from the last element. If not set, the function will remove all elements from start-parameter.
arrayOptional.Specifies an array to be inserted to the original array. If it's only one element, it can be a string.

Note

When inserting an array, the keys of the inserted elements aren't preserved. They're reindexed using numeric keys. So array_splice() isn't that useful for inserting associative arrays. For example:


<?PHP//from j  av  a2 s .  co  m
$authors = array( "Java", "PHP", "CSS" ); 
array_splice( $authors, 1, 0, array( "authorName" => "MySQL" ) ); 
print_r( $authors ); 
?>

Notice how the "MySQL" element has had its original key ( "authorName") replaced with a numeric key (1).

The code above generates the following result.

Example 1

Replace elements in an array


<?php/*j a v a  2s .  co  m*/
    $a1=array("a"=>"A","b"=>"B","c"=>"C","d"=>"java2s.com");
    $a2=array("a"=>"A","b"=>"B");
    array_splice($a1,0,2,$a2);
    print_r($a1);
?>

The code above generates the following result.

Example 2

With the length parameter set to 0:


<?php// jav  a  2 s .c o m
    $a1=array("0"=>"Java","1"=>"java2s.com");
    $a2=array("0"=>"PHP","1"=>"CSS");
    array_splice($a1,1,0,$a2);
    print_r($a1);
?>

The code above generates the following result.

Example 3

Adding two new elements to the middle


<?php //  java 2  s  . c  o m

       $authors = array( "Java", "PHP", "CSS" ); 
       $arrayToAdd = array( "Python", "Javascript" ); 
        
       print_r( $authors ); 
       
       print_r( array_splice( $authors, 2, 0, $arrayToAdd ) ); 
       
       print_r( $arrayToAdd ); 
       
       print_r( $authors ); 

?> 

The code above generates the following result.

This example inserts two new elements at the third position in the array, displaying the removed elements, which in this case is an empty array because no elements were removed:

print_r( array_splice( $authors, 2, 0, $arrayToAdd ) );

You can read this line as: "At the third position (2), remove zero (0) elements, then insert $arrayToAdd".

Example 4

Remove and insert elements at the same time:

print_r( array_splice( $authors, 0, 2, $arrayToAdd ) );

This code removes two elements from the start of the array (position 0), then inserts the contents of $arrayToAdd at position 0.


<?php //from jav  a  2  s  . com
      echo "2. Replacing two elements with a new element"; 

      $authors = array( "Java", "PHP", "CSS" ); 
      $arrayToAdd = array( "Oracle" ); 
       
      print_r( $authors ); 
       
      print_r( array_splice( $authors, 0, 2, $arrayToAdd ) ); 
       
      print_r( $arrayToAdd ); 
       
      print_r( $authors ); 
       
?> 

The code above generates the following result.

Example 5

What happens if you omit the third argument:

print_r( array_splice( $authors, 1 ) );

This code removes all the elements from the second position in the array (position 1) to the end of the array.


<?php /*from   java2  s. c om*/
      echo "3. Removing the last two elements"; 

      $authors = array( "Java", "PHP", "CSS" ); 
      print_r( $authors ); 
      print_r( array_splice( $authors, 1 ) ); 
      print_r( $authors ); 

?> 

The code above generates the following result.

Example 6

You don't have to pass an array as the fourth argument. If you only have one element to add, just pass the value.

array_splice() automatically casts the fourth argument to an array before using it.

print_r( array_splice( $authors, 1, 0, "Ruby" ) );


<?php /*from j  a  v a  2s . com*/
      echo "4. Inserting a string instead of an array"; 

      $authors = array( "Java", "PHP", "CSS" ); 
      print_r( $authors ); 
      print_r( array_splice( $authors, 1, 0, "Ruby" ) ); 
      print_r( $authors ); 
?> 

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP array_sum() Function
  2. Syntax for PHP array_sum() Function
  3. Parameter for PHP array_sum() Function
  4. Example - Sum an indexed array
  5. Example - Sum values in an associate array
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function