PHP | array_slice() Function

The array_slice() is an inbuilt function of PHP and is used to fetch a part of an array by slicing through it, according to the users choice.

Syntax:

array_slice($array, $start_point, $slicing_range, preserve)

Parameters: This function can take four parameters and are described below:



  1. $array (mandatory): This parameter refers to the original array, we want to slice.
  2. $start_point (mandatory): This parameter refers to the starting position of the array from where the slicing need to be performed. It is mandatory to supply this value. If the value supplied is negative, then the function starts slicing from the end of the array, i.e., -1 refers to the last element of the array.
  3. $slicing _range (optional): This parameter refers to the range or limit point upto which the slicing is needed to be done. A negative value will indicate the count from the end of the string. Now, this can also be left blank. On leaving blank the function will slice through all the values as mentioned in the starting point right up to the end.
  4. preserve (optional): This parameter can take only two boolean parameters, i.e., either True or False. This will tell the function whether to preserve the keys or reset it. True refers to preserve the keys and false refers to reset the keys. False being the default value.

Return Value: As already mentioned this function will return the slected or the sliced parts of the array.

Below program illustrate the array_slice() function in PHP:

  • In this program, we will be passing all the positive parameters along with the True value to preserve the keys.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // PHP program to illustrate the 
    // array_slice() function
      
    // Input array
    $array = array("ram", "krishna", "aakash",
                            "gaurav", "raghav");
                              
    // Slice from pos 1 to pos 3                        
    print_r(array_slice($array, 1, 3, true));
      
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [1] => krishna
        [2] => aakash
        [3] => gaurav
    )
  • Now let’s try to observe the output by passing the same values as in above program, but with False as the value of the preserved key.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // PHP program to illustrate the
    // array_slice() function
      
    // input array
    $array = array("ram", "krishna", "aakash"
                            "gaurav", "raghav");
      
    // Slice from pos 1 to pos 3
    print_r(array_slice($array, 1, 3, false));
      
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [0] => krishna
        [1] => aakash
        [2] => gaurav
    )
    
  • Below program shows what happens when we dont give the range parameter:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // PHP program to illustrate the 
    // use of array_slice()
      
    // Input array
    $array = array("ram", "krishna", "aakash",
                            "gaurav", "raghav");
      
    // Slice from pos 1 to end
    print_r(array_slice($array, 1));
      
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [0] => krishna
        [1] => aakash
        [2] => gaurav
        [3] => raghav
    )
    
  • Below program illustrate the array_slice() function when we pass the negative parameter as our starting position:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // PHP program to illustrate the 
    // use of array_slice()
      
    // Input array
    $array = array("ram", "krishna", "aakash"
                            "gaurav", "raghav");
      
    // Slice from pos 3rd position to 
    // the end of the array
    print_r(array_slice($array, -3));
      
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [0] => aakash
        [1] => gaurav
        [2] => raghav
    )
    
  • Below program shows what happens when we try to pass the negative parameters as both the starting point and the length or range:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // PHP program to illustrate the 
    // use of array_slice()
      
    // Input Array
    $array = array("ram", "krishna", "aakash"
                            "gaurav", "raghav");
      
    // Slice from pos 1 to end
    print_r(array_slice($array, -3, -2, true));
      
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [2] => aakash
    )
    

Reference:
http://php.net/manual/en/function.array-slice.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.