Skip to content
Home » Functions » array_slice: How to use with examples

array_slice: How to use with examples

What is PHP array_slice?

Description

PHP array_slice extracts a slice of the array.

Function Signature

array_slice( array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array

Arguments

  • $array – Input array.
  • $offset – Position in the array.
    • Positive offset, the slicing will start at the offset in the array.
    • Negative offset, the slicing will start that far from the end of the array.
  • $length
    • Omitted length: The slice will include everything from the offset to the end of the array.
    • Positive length: The slice will include up to that many elements.
    • Negative length: The slice will exclude up to that many elements from the array’s end.
  • $preserve key – If true, the slice retains the original numeric index in the slice

Return Type

The function returns the slice or an empty array if the offset is larger than the array’s length

Notes

  • Only the available elements will be returned if the $length is greater than the array’s length.
  • The array_slice reorders and resets the numeric index. To change this default behavior, pass true for $preserve_keys.
  • The function preserves the string keys regardless of the $preserve_keys value.

Introduction – PHP array_slice

PHP array_slice is a handy function to get a subset of an array. It takes an array and the offset and length values and returns a slice. The offset represents the starting position of the slice, and the positive length represents the count of the slice. Length value can be negative, and in that case, it excludes that many elements from the end of an array.

This function is quite dynamic in the sense that it can slice an array from virtually everywhere based on the offset and length arguments. So, it is essential to experiment with different offset and length values, and that is what this article does. Besides, the article also includes examples where the array_slice does basic array operations, like returning the first or last element of an array. So, let’s begin without waiting any further.

Usage Examples – PHP array_slice

#1 – Positive offset and length

A positive offset represents a position from the start of an array. The length represents the count of elements from the offset. Let’s try this combination on an array.

<?php
$arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
 
print_r(array_slice($arr, 1, 3));
 
/*
OUTPUT
Array
(
    [0] => B
    [1] => C
    [2] => D
)
*/
 
?>

#2 – Positive offset and negative length

So, with the negative length, the function excludes that many elements from the end of the array while slicing it.

PHP array_slice
<?php
$arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
 
print_r(array_slice($arr, 1, -7));
 
/*
OUTPUT
Array
(
    [0] => B
    [1] => C
    [2] => D
)
*/
 
?>

#3 – Negative offset and positive length

The negative offset locates an array element from the end of an array. So, a -1 represents the last element of an array, -2 represents the second-last, and so on.

<?php
$arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
 
print_r(array_slice($arr, -5, 3));
 
/*
OUTPUT
Array
(
    [0] => G
    [1] => H
    [2] => I
)
*/
 
?>

#4 – Negative offset and negative length

So, the negative offset locates an element from the end of an array. The negative length excludes that many elements from the end of the array.

PHP array_slice
<?php
$arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
 
print_r(array_slice($arr, -7, -4));
 
/*
OUTPUT
Array
(
    [0] => E
    [1] => F
    [2] => G
)
*/
 
?>

Voila! These examples cover all the offset and length values combinations and explore different possibilities of slicing an array.

Preserve Keys in PHP array_slice

The array_slice preserves string keys by default. The function should get true for the preserve_keys argument to keep the numeric keys intact. Here’s an example.

<?php
$arr = ["F"=> "Facebook", "A"=> "Apple", "A" => "Amazon", "N" => "Netflix", "G" => "Google", 42 => "Twitter" ];
 
print_r(array_slice($arr, 0, count($arr), $preserve_keys = true));
 
/*
OUTPUT
Array
(
    [F] => Facebook
    [A] => Amazon
    [N] => Netflix
    [G] => Google
    [42] => Twitter
)
*/
 
?>

Return first or last array element with array_slice

The examples show how the array_slice function can return an array’s first or last element.

<?php
$arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"];
 
 
print_r(array_slice($arr,0,1)[0]); //A
 
 
print_r(array_slice($arr, -1, 1)[0]); //K
?>

 Voila! That’s all for array_slice PHP.

Conclusion

This article explores PHP array_slice function with various examples. These examples try different offset and length combinations, revealing numerous possibilities of slicing an array. Besides, the article shows how to preserve keys using the array_slice. Finally, an example demonstrates how to get the first or last element of an array using array_slice. Hopefully, you’ve learned something new today. If you like the article, perhaps you’ll love other content too at FuelingPHP. Do check them out!

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.