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

array_slice

Description

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

1 · array offset · Negative

<?

$array = array("a", "b", "c", "d", "e");
$offset = -1;

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

print_r($return);

?>
Array
(
    [0] => e
)

2 · array offset · Non-negative

<?

$array = array("a", "b", "c", "d", "e");
$offset = 1;

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

print_r($return);

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

3 · length · Negative

<?

$array = array("a", "b", "c", "d", "e");
$offset = 0;
$length = -2;

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

print_r($return);

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

4 · length · Positive

<?

$array = array("a", "b", "c", "d", "e");
$offset = 0;
$length = 2;

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

print_r($return);

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

5 · length · >Array

<?

$array = array("a", "b", "c", "d", "e");
$offset = 0;
$length = 10;

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

print_r($return);

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

6 · preserve_keys

<?

$array = array("a", "b", "c", "d", "e");
$offset = 1;
$length = 2;
$preserve_keys = true;

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

print_r($return);

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

7 · One-based

<?

// offset denotes position not key

$array = array(1 => "a", "b", "c", "d", "e");
$offset = 1;

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

print_r($return);

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

8 · Mixed Keys

<?

$array = array('a' => 'apple', 'b' => 'banana', '42' => 'pear', 'd' => 'orange');
$offset = 0;
$length = 3;
$preserve_keys = true;

$return1 = array_slice($array, $offset, $length);
$return2 = array_slice($array, $offset, $length, $preserve_keys);

print_r($return1);
print_r($return2);

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