What is PHP array_slice() Function?
If you want to extract a portion from an array, use array_slice() function. The extracted portion is a collection of consecutive elements of the array.
The function cuts a slice of the array, hence the name “array_slice”.
Syntax:
array_slice(array, starting_position, length_to_extract_or_omit, preserve_keys)
Parameters:
The Function has 2 required parameters and 2 optional parameters-
array (Required): Array to be operated on.
starting_position (Required): It refers to the position to start extracting in the array. It could be a positive or negative number-
- For positive starting_position: It indicates the starting_positionth element as the first element to start extracting (Check row 1 of Table 1). Note, the position of the first element in the array is considered as zero.
- For negative starting_position: It indicates the starting_positionth element starting from the end of the array as the first element to start extracting (Check row 2 of Table 1).
- For zero starting_position: It refers to the starting_positionth character as the first element to start extracting (Check row 3 of Table 1).
- If starting_position is greater in number than the array length: The function returns empty array (Check row 4 of Table 1).
Check example 1.
Table 1:
Array | Example | Result | |
1 | $arr = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’] | array_slice($arr, 2) | [’c’,’d’,’e’,’f’,’g’,’h’] |
2 | $arr = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’] | array_slice($arr, -2) | [’g’,’h’] |
3 | $arr = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’] | array_slice($arr, 0) | [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’] |
4 | $arr = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’] | array_slice($arr, 20) | [] |
length_to_extract_or_omit (optional): It could be a positive or negative number-
- For positive length_to_extract_or_omit: The function extracts and returns length_to_extract_or_omit elements starting from the starting_position of the array (Check row 1 of Table 2). If there are not enough elements to be extracted, the function returns as many elements as exist (Check row 2 of Table 2).
- For negative length_to_extract_or_omit: The function omits length_to_extract_or_omit elements from the end of the array and returns the remaining elements (Check row 3 of Table 2).
- For zero length_to_extract_or_omit: The function returns an empty array (Check row 4 of Table 2).
- For NULL or omitted length_to_extract_or_omit: The function returns an array starting from starting_position to the end (Check row 5 of Table 2).
Check example 2.
Table 2:
String | Example | Result | |
1 | $arr = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’] | array_slice($arr, 3,1) | [’d’] |
2 | $arr = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’] | array_slice($arr, 3,10) | [’d’,’e’,’f’,’g’,’h’] |
3 | $arr = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’] | array_slice($arr, 3,-2) | [’d’,’e’,’f’] |
4 | $arr = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’] | array_slice($arr, 3, 0) | [] |
5 | $arr = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’] | array_slice($arr, 3, NULL) | [’d’,’e’,’f’,’g’,’h’] |
preserve_keys (Optional): It instructs the function whether to preserve the keys or not. This parameter can have two values-
- TRUE – It tells the function to preserve the keys in the extracted array.
- FALSE – It tells the function not to preserve the keys in the extracted array, rather reset the keys. Check example 3. Note: the function preserves keys for the associative arrays (string keys) even though you mention FALSE in this parameter. Check example 4. This is the default value that mean if you don’t mention this parameter, the function doesn’t preserve keys for the indexed array.
Return Values:
The function returns the extracted portion of the array or an empty array if the starting_position is larger than the array size.
Examples:
Example 1:
<pre>
<?php
$arr=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
print_r(array_slice($arr,2));
print_r(array_slice($arr,-2));
print_r(array_slice($arr,0));
print_r(array_slice($arr,20));
?>
</pre>
Output:
Array
(
[0] => c
[1] => d
[2] => e
[3] => f
[4] => g
[5] => h
)
Array
(
[0] => g
[1] => h
)
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
)
Array
(
)
Example 2:
<pre>
<?php
print_r(array_slice($arr,3, 1));
print_r(array_slice($arr,3, 10));
print_r(array_slice($arr,3, -2));
print_r(array_slice($arr,3, 0));
print_r(array_slice($arr,3, NULL));
?>
</pre>
Output:
Array
(
[0] => d
)
Array
(
[0] => d
[1] => e
[2] => f
[3] => g
[4] => h
)
Array
(
[0] => d
[1] => e
[2] => f
)
Array
(
)
Array
(
[0] => d
[1] => e
[2] => f
[3] => g
[4] => h
)
Example 3:
<pre>
<?php
$arr=['a', 'b', 'c', 'd', 'e'];
print_r(array_slice($arr, 3, 2, TRUE));
?>
</pre>
Output:
Array
(
[3] => d
[4] => e
)
Example 4:
<pre>
<?php
$arr=['a' => 'Ada', 'b' => 'Basic', 'c' => 'CSS'];
print_r(array_slice($arr, 1, 2, FALSE));
?>
</pre>
Output:
Array
(
[b] => Basic
=> CSS
)
Example 5:
<pre>
<?php
$arr=['a', 'b', 'c', 'd'];
array_slice($arr,2);
print_r($arr);
?>
</pre>
Output:
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
Notes on array_slice() Function:
- Though the function extracts a portion from the array, it doesn’t modify the original array. Check example 5.
- If you don’t provide the third parameter (length_to_extract_or_omit), the extraction continues to the end of the array. Check example 1.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP array_slice() Function
Whenever you need a portion of an array, use PHP array_slice() function. It is a very useful function when you work with array. This is a built-in PHP array function.