HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

array_slice

Description

The array_slice of Array for PHP extracts 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.

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

Default is false which will reindex the slice numerically. Keys will be preserved when set to true. String keys are always preserved.

Return

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

Examples

1 · array offset · negative

<?

$array =
[
    "a",
    "b",
    "c",
    "d",
    "e",
    "f"
];
$offset = -2;

$return = array_slice($array, $offset);

print_r($return);
Array
(
    [0] => e
    [1] => f
)

2 · array offset · zero

<?

$array =
[
    "a",
    "b",
    "c",
    "d",
    "e",
    "f"
];
$offset = 0;

$return = array_slice($array, $offset);

print_r($return);
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)

3 · array offset · positive

<?

$array =
[
    "a",
    "b",
    "c",
    "d",
    "e",
    "f"
];
$offset = 2;

$return = array_slice($array, $offset);

print_r($return);
Array
(
    [0] => c
    [1] => d
    [2] => e
    [3] => f
)

4 · length · negative

<?

$array =
[
    "a",
    "b",
    "c",
    "d",
    "e",
    "f"
];
$offset = 0;
$length = -2;

$return = array_slice($array, $offset, $length);

print_r($return);
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

5 · length · zero

<?

$array =
[
    "a",
    "b",
    "c",
    "d",
    "e",
    "f"
];
$offset = 0;
$length = 0;

$return = array_slice($array, $offset, $length);

print_r($return);
Array
(
)

6 · length · positive

<?

$array =
[
    "a",
    "b",
    "c",
    "d",
    "e",
    "f"
];
$offset = 0;
$length = 2;

$return = array_slice($array, $offset, $length);

print_r($return);
Array
(
    [0] => a
    [1] => b
)

7 · length > array

<?

$array =
[
    "a",
    "b",
    "c",
    "d",
    "e",
    "f"
];
$offset = 0;
$length = 100;

$return = array_slice($array, $offset, $length);

print_r($return);
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)

8 · preserve_keys · false

<?

$array =
[
    "a",
    "b",
    "c" => "c",
    "d",
    "e",
    "f"
];
$offset = 1;
$length = 3;
$preserve_keys = false;

$return = array_slice($array, $offset, $length, $preserve_keys);

print_r($return);
Array
(
    [0] => b
    [c] => c
    [1] => d
)

9 · preserve_keys · true

<?

$array =
[
    "a",
    "b",
    "c" => "c",
    "d",
    "e",
    "f"
];
$offset = 1;
$length = 3;
$preserve_keys = true;

$return = array_slice($array, $offset, $length, $preserve_keys);

print_r($return);
Array
(
    [1] => b
    [c] => c
    [2] => d
)