array_reverse

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

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
)

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
)

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
)

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
)

PHP

Home Menu