English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The PHP array_slice() function extracts a segment from an array
array_slice($array, $offset [,$length [,$preserve_keys]] );
array_slice() returns a segment of the array specified by the offset and length parameters.
Serial Number | Parameters and Description |
---|---|
1 | array(必填) It specifies an array. |
2 | offset(必填) 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. |
3 | length(可选) If length is given and is positive, the sequence will have this many units. If length is given and is 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. |
4 | preserve_keys(可选) Note that array_slice() will by default reorder and reset the numeric indices of the array. You can change this behavior by setting preserve_keys to TRUE. |
It returns a sequence of elements. If the offset parameter is greater than the size of the array, an empty array will be returned.
Return a Segment of Array Elements
<?php $input = array("a", "b", "c", "d", "e"); print_r(array_slice($input,", 2, -1)); print_r(array_slice($input,", 2, -1, true)); ?>Test and See‹/›
Output Result:
Array ( [0]]=>c [1]]=>d ) Array ( [2]]=>c [3]]=>d )