In Working With Array Elements in PHP, you saw how to read and change individual elements within a PHP array. However, sometimes it’s useful to work with a range of array elements. For example, you may want to work with a large array in chunks, or sort an array then retrieve just the “top 10” values from the start of the array.
In this tutorial you’ll explore PHP’s array_slice()
function for extracting a range of elements from an array.
Basic array_slice()
usage
To use array_slice()
, you typically pass it the following arguments:
- The array to extract the slice from.
- The position of the element where you want to start the extraction (the first element has a position of
0
). - The number of elements to extract.
array_slice()
then returns an array containing the extracted elements. (The original array is not modified.)
Here’s an example showing how to use array_slice()
:
$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );
// Displays "Array ( [0] => Stanley Kubrick [1] => Martin Scorsese )"
print_r( array_slice( $directors, 1, 2 ) );
The above code creates a 4-element array of director names, then uses array_slice()
to extract the second and third elements.
Note that the position of an element is not necessarily the same as its index. For example, the first element in an array always has a position of 0
, but its index might be 456
. Indexed arrays in PHP do not have to have contiguous indices starting from zero (although they often do).
Preserving indices
You can see from the above example that array_slice()
re-indexes the elements in the resulting array: Stanley Kubrick gets a new index of 0
, while Martin Scorsese gets a new index of 1
. Often this isn’t a problem, since you only care about the order of the elements in the resulting slice, not their indices.
However, sometimes it’s important to retain the indices of the extracted elements. For example, the indices might be keys that point to records in a database table, or they might have some other significance. In this situation, you can preserve the indices of the extracted elements by passing a fourth argument of true
to array_slice()
. Here’s an example:
$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );
// Displays "Array ( [1] => Stanley Kubrick [2] => Martin Scorsese )"
print_r( array_slice( $directors, 1, 2, true ) );
Notice that array_slice()
has now preserved the indices from the original array: 1
for Stanley Kubrick, and 2
for Martin Scorsese.
array_slice()
always preserves indices in associative arrays, as you’ll see in a moment. So there’s no need to pass the true
argument when slicing associative arrays.
Extracting to the end of an array
If you leave out the third argument then array_slice()
extracts from the starting position all the way to the end of the array. This can be useful when you don’t know how long the array is. For example:
$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );
// Displays "Array ( [0] => Stanley Kubrick [1] => Martin Scorsese [2] => Fritz Lang )"
print_r( array_slice( $directors, 1 ) );
Using array_slice()
on associative arrays
You can also use array_slice()
to extract elements from an associative array. Here’s an example that retrieves 2 elements from an associative array, starting at the second element:
$movie = array( "title" => "Rear Window",
"director" => "Alfred Hitchcock",
"year" => 1954,
"minutes" => 112 );
// Displays "Array ( [director] => Alfred Hitchcock [year] => 1954 )"
print_r( array_slice( $movie, 1, 2 ) );
Notice that, in the above example, array_slice()
preserves the "director"
and "year"
indices in the resulting array.
Summary
In this article you’ve explored array_slice()
, a handy PHP function that returns a range of elements in an array. You’ve seen how to:
- Use
array_slice()
with both indexed and associative arrays - Preserve indices when working with indexed arrays, and
- Extract elements all the way up to the end of an array.
Happy coding!
Leave a Reply