Jump to: navigation, search

Php array reverse

From w3cyberlearnings

Contents

PHP function array_reverse

This function reverses array.

Syntax array_reverse

  • array: array input
  • bool (option): TRUE (preserved the numeric keys)
array_reverse(array, bool);

Example 1

<?php

$person = array('giant','emissions','news','animals');

$r = array_reverse($person);
print_r($r);
?>


Output


Array
(
    [0] => animals
    [1] => news
    [2] => emissions
    [3] => giant
)

Example 2: preserved the keys

<?php

$person = array('giant','emissions','news','animals');

$r = array_reverse($person,true);
print_r($r);
?>

Output

Array
(
    [3] => animals
    [2] => news
    [1] => emissions
    [0] => giant
)

Example 3

<?php

$list = array('apple' => 'dog', 3 => 'great', 'love');
$new_list = array_reverse($list);

print_r($new_list);
?>

Output

Array
(
    [0] => love
    [1] => great
    [apple] => dog
)


Related Links


Navigation
Web
SQL
MISC
References