English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Online tools

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

Object function

PHP Array Function Manual

PHP array_slice() function usage and example

The PHP array_slice() function extracts a segment from the array

Syntax

array_slice($array, $offset [,$length [,$preserve_keys]]);

 Definition and usage

array_slice() returns a segment of the array array specified by the offset and length parameters.

ParameterSerial number
1

Parameter and description

array (required)

2

It specifies an array.

offset (required)

3

If offset is non-negative, the sequence will start from this offset in the array. If offset is negative, the sequence will start from a distance from the end of the array.

length (optional)

4

If length is given and is positive, the sequence will have this many units. If length is given and negative, the sequence will terminate at a distance from the end of the array. If omitted, the sequence will start from offset to the end of the array.

preserve_keys (optional)

Note that array_slice() by default will re-sort and reset the numeric indices of the array. You can change this behavior by setting preserve_keys to TRUE.

Return value

It returns the sequence of elements. If the offset parameter is greater than the array size, an empty array will be returned.

Online example

Example
   <?php
   
   print_r(array_slice($input, "", )); 2, -1$input = array("a", "b", "c", "d", "e");
   print_r(array_slice($input, "", )); 2, -1, true));
?>
Test and see‹/›

Output result:

Array
(
    [0] => c
    [1=> d
)
Array
(
    [2=> c
    [3=> d
)

PHP Array Function Manual