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

array_reverse

Description

The array_reverse of Array for PHP return an array with elements in reverse order.

Syntax

array_reverse(
    array $array,
    bool $preserve_keys = false
): array

Parameters

array

The input array.

preserve_keys

If set to true numeric keys are preserved. Non-numeric keys are not affected by this setting and will always be preserved.

Return

Returns the reversed array.

Examples

1 · array · Indexed

<?

$array  = array(0, 1, array(2, 3));

$return = array_reverse($array);

print_r($return);

?>
Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 3
        )

    [1] => 1
    [2] => 0
)

2 · array · Associative

<?

$array  = array("a" => 0, "b" => 1, "c" => array("d" => 2, "e" => 3));

$return = array_reverse($array);

print_r($return);

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

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

3 · preserve_keys · Indexed

<?

$array  = array(0, 1, array(2, 3));
$preserve_keys = true;

$return = array_reverse($array, $preserve_keys);

print_r($return);

?>
Array
(
    [2] => Array
        (
            [0] => 2
            [1] => 3
        )

    [1] => 1
    [0] => 0
)

4 · preserve_keys · Associative

<?

$array  = array("a" => 0, "b" => 1, "c" => array("d" => 2, "e" => 3));
$preserve_keys = true;

$return = array_reverse($array, $preserve_keys);

print_r($return);

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

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