Syntax
The syntax for the array_slice function in PHP is :
array_slice(array $array, int $offset, int $length = null, bool $preserve_keys = false): array
In this Syntax :
- `$array`: The input array from which the slice will be extracted.
- `$offset`: The starting index of the slice. It can be a positive or negative integer.
- If `$offset` is positive, the slice will start from that index in the array.
- If `$offset` is negative, the slice will start from that many elements from the end of the array.
- `$length` (optional): The length or number of elements to include in the slice.
- If `$length` is given and is positive, the slice will have up to that many elements.
- If `$length` is given and is negative, the slice will stop that many elements from the end of the array.
- If `$length` is not provided or is null, the slice will extend to the end of the array.
- `$preserve_keys` (optional): A boolean value indicating whether to preserve the original keys of the sliced elements.
- If set to true, the original keys of the sliced elements will be preserved in the returned array.
- If set to false (default), the returned array will have numerically reindexed keys.
The array_slice function returns a new array containing the extracted slice from the input array.
Parameters
Let’s discuss the parameters of the array_slice function in more detail :
1. `$array`
- The `$array` parameter is the input array from which the slice will be extracted.
- It can be an indexed array or an associative array.
- The array_slice function works with both types of arrays.
2. `$offset`
- The `$offset` parameter specifies the starting index of the slice.
- It can be a positive or negative integer.
- If `$offset` is positive, the slice will start from that index in the array. For example, an `$offset` of 2 means the slice will start from the third element (index 2) of the array.
- If `$offset` is negative, the slice will start from that many elements from the end of the array. For example, an `$offset` of -3 means the slice will start from the third element from the end of the array
3. `$length` (optional)
- The `$length` parameter specifies the number of elements to include in the slice.
- It is an optional parameter and can be omitted.
- If `$length` is given and is positive, the slice will have up to that many elements. For example, a `$length` of 4 means the slice will contain a maximum of 4 elements.
- If `$length` is given and is negative, the slice will stop that many elements from the end of the array. For example, a `$length` of -2 means the slice will exclude the last 2 elements of the array.
- If `$length` is not provided or is null, the slice will extend to the end of the array.
4. `$preserve_keys` (optional)
- The `$preserve_keys` parameter is a boolean value that determines whether to preserve the original keys of the sliced elements.
- It is an optional parameter and defaults to false.
- If set to true, the original keys of the sliced elements will be preserved in the returned array. This means that if the input array has string keys or non-sequential numeric keys, they will be maintained in the slice.
- If set to false (default), the returned array will have numerically reindexed keys starting from 0.
Return Value
The array_slice function returns a new array containing the extracted slice from the input array. The returned array includes the elements starting from the specified `$offset` and up to the specified `$length` (if provided).
Let’s understand few important points about the return value:
1. The returned array is a new array:
- The array_slice function does not modify the original input array.
- Instead, it creates a new array that contains the extracted slice.
- This means you can safely use array_slice without worrying about modifying the original array.
2. The keys of the returned array depend on the `$preserve_keys` parameter:
- If `$preserve_keys` is set to true, the original keys of the sliced elements will be preserved in the returned array.
- If `$preserve_keys` is set to false (default), the returned array will have numerically reindexed keys starting from 0.
3. If the `$offset` is outside the bounds of the array:
- If `$offset` is greater than the size of the array, an empty array will be returned.
- If `$offset` is negative and its absolute value exceeds the size of the array, an empty array will be returned.
4. If the `$length` is specified and exceeds the available elements:
- If `$length` is positive and exceeds the number of elements available from the `$offset`, the slice will extend to the end of the array.
- If `$length` is negative and its absolute value exceeds the number of elements available from the `$offset`, the slice will include all elements from the `$offset` to the end of the array.
Note: The returned array can be assigned to a variable or used directly in further operations, like iterating over its elements, passing it to other functions, or manipulating it as needed.
Examples
Now that we have discussed all the components of this function in detail let's look at the few examples to understand this functions’ implementation properly :
Example 1: Extracting a slice from an indexed array
$fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
$slice = array_slice($fruits, 1, 3);
print_r($slice);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => banana
[1] => orange
[2] => grape
)
In this example, we have an indexed array `$fruits` containing various fruit names. We use array_slice to extract a slice starting from index 1 with a length of 3. The resulting `$slice` array contains the elements 'banana', 'orange', & 'grape'.
Example 2: Extracting a slice with a negative offset
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$slice = array_slice($numbers, -5, 3);
print_r($slice);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[0] => 6
[1] => 7
[2] => 8
)
In this example, we have an indexed array `$numbers` containing numbers from 1 to 10. We use array_slice with a negative offset of -5, which means the slice will start from the 5th element from the end of the array. The length is set to 3, so the slice will include the next 3 elements. The resulting `$slice` array contains the elements 6, 7, & 8.
Example 3: Preserving keys while extracting a slice
$person = [
'name' => 'Ravi',
'age' => 25,
'city' => 'New Delhi',
'country' => 'India'
];
$slice = array_slice($person, 1, 2, true);
print_r($slice);

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[age] => 25
[city] => 'New Delhi'
)
In this example, we have an associative array `$person` containing information about a person. We use array_slice to extract a slice starting from index 1 with a length of 2. The `$preserve_keys` parameter is set to true, so the original keys of the sliced elements are preserved in the resulting `$slice` array.
Frequently Asked Questions
What happens if the `$offset` is greater than the size of the array?
If the `$offset` is greater than the size of the array, array_slice will return an empty array.
Can array_slice be used with associative arrays?
Yes, array_slice works with both indexed and associative arrays. By default, the keys are not preserved, but you can set the `$preserve_keys` parameter to true to retain the original keys.
Does array_slice modify the original array?
No, array_slice does not modify the original array. It returns a new array containing the extracted slice, leaving the original array unchanged.
Conclusion
In this article, we have discussed the array_slice function in PHP. We learned that array_slice allows us to extract a portion of an array based on a starting offset and an optional length. We explained the syntax, parameters, and return value of array_slice and provided examples to show its usage. Whether you need to extract a subset of elements from an indexed array or an associative array, array_slice provides a convenient way to achieve that without modifying the original array.
You can also check out our other blogs on Code360.