Menu
Riverview Church

ARRAY_SLICE

Extract a slice of the array

SYNTAX

array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) : array

PARAMETERS

array

The input array.

offset

If offset is non-negative, the sequence will start at that offset in the array.

If offset is negative, the sequence will start that far from the end of the array.

Note: The offset parameter denotes the position in the array, not the key.

length

If length is omitted, then the sequence will have everything from offset up until the end of the array.

If length is positive, then the sequence will have up to that many elements in it.

If length is negative, then the sequence will stop that many elements from the end of the array.

If length is longer than the array, then only the available array elements will be present.

preserve_keys

Note: array_slice() will reorder and reset the integer array indices by default. This behaviour can be changed by setting preserve_keys to TRUE. String keys are always preserved, regardless of this parameter.

RETURN

Returns the slice. If the offset is larger than the size of the array, an empty array is returned.

EXAMPLES

ARRAY OFFSET | NEGATIVE

Array
(
    [0] => e
)

ARRAY OFFSET | NON-NEGATIVE

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

LENGTH | NEGATIVE

Array
(
    [0] => a
    [1] => b
    [2] => c
)

LENGTH | POSITIVE

Array
(
    [0] => a
    [1] => b
)

LENGTH | >ARRAY

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)

PRESERVE_KEYS

Array
(
    [1] => b
    [2] => c
)

ONE-BASED

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

MIXED KEYS

Array
(
    [a] => apple
    [b] => banana
    [0] => pear
)
Array
(
    [a] => apple
    [b] => banana
    [42] => pear
)

INTERNAL

array_chunk

array_splice

EXTERNAL

PHP