PHP array_slice Function is an inbuilt Function in PHP. It is used to fetch a part of an array by slicing through it. In this article, we will discuss the array_slice Function in PHP along with some examples.
array_slice($array, $startPoint, $range, preserveKeys)
The PHP array_slice Function consists of four parameters. Two parameters are mandatory and the other two are optional. Let’s discuss the parameters required in the array_slice Function in PHP below:
- $array: The first parameter is the mandatory array which we want to slice.
- $startPoint: The starting point denotes to the index from where the function will begin slicing. If the
startPointis positive, the sequence starts from the beginning of the array. However, it is negative then it starts from the end of the array. For example, -1 refers to the last element.
- $range: The range is an optional parameter in the function. If the value of the range is positive then the array_slice function will slice the array up to that given range. If the value is negative, then the function will slice up to the last element from the end of the array. However, if the range is not passed, then the function will slice up to the end of the array.
- $preserveKeys: This parameter can either be true or false. If the preserveKeys is set then the original keys of the array are preserved. However, by default the value of this parameter is false. Hence, the PHP array_slice Function will reorder and reset the integer array indices by default. Note: String keys are always preserved, regardless of the parameter.
The PHP array_slice Function returns a sliced array. However, it returns an empty array if the offset is larger than the size of the array.
Let’s discuss a few examples to demonstrate the usage of PHP array_slice Function.
Example 1: Positive Parameters and Integer Keys
<?php $testArray = array('apple', 'banana', 'mango', 'grapes', 'orange'); print_r(array_slice($testArray, 2)); //Array (  => mango  => grapes  => orange ) print_r(array_slice($testArray, 2, 1)); //Array (  => mango ) ?>
In the above example, the function returns an array after slicing the original array.
Example 2: Negative Parameters and Integer Keys
For instance, consider the example.
<?php $testArray = array('apple', 'banana', 'mango', 'grapes', 'orange'); print_r(array_slice($testArray, -2, -1)); //Array (  => grapes  => orange ) print_r(array_slice($testArray, 2, -1)); //Array (  => mango  => grapes ) print_r(array_slice($testArray, -2, 1)); //Array (  => grapes ) ?>
On passing negative startPoint the slicing begins from the end of the array. Also, the negative range makes the slicing up to the last element from the end.
Example 3: Mixed Keys
For instance, consider slicing on an array containing mixed keys.
<?php $testArray = array('a' => 'apple', 'b'=>'banana', 4 =>'mango', 'g' => 'grapes', '1' => 'orange'); print_r(array_slice($testArray, 1)); //Array ( [b] => banana  => mango [g] => grapes  => orange ) print_r(array_slice($testArray, 1, 5, true)); //Array ( [b] => banana  => mango [g] => grapes  => orange ) ?>
The above example shows the significance of preserveKeys parameter. When the preserve keys are not passed, then the integer keys are reset in the new array. On passing
n this article, we discussed about PHP array_slice Function. You can read about other PHP Array Functions on Concatly.
Also, please refer to the PHP Official Documentation for more information.
Vishesh is currently working as an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.