PHP array_slice Function | Extract Slice of Array

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.

Syntax

array_slice($array, $startPoint, $range, preserveKeys)

Parameters

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 startPoint is 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.

Return Value

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.

PHP array_slice Function Diagram

Examples

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 ( [0] => mango [1] => grapes [2] => orange )

	print_r(array_slice($testArray, 2, 1)); //Array ( [0] => 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 ( [0] => grapes [1] => orange )

	print_r(array_slice($testArray, 2, -1)); //Array ( [0] => mango [1] => grapes )

	print_r(array_slice($testArray, -2, 1)); //Array ( [0] => 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 [0] => mango [g] => grapes [1] => orange )

	print_r(array_slice($testArray, 1, 5, true)); //Array ( [b] => banana [4] => mango [g] => grapes [1] => 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 preserveKeys, the final array retains all the original keys. However, string keys are always preserved regardless of the parameter passed.

Conclusion

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.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *