PHP array_reverse() function
This function reverses the order of elements in an array.
Syntax
array_reverse(array, preserve_key);
The parameter 'array' specifies the name of the array. 'preserve_key' is an optional parameter. It specifies whether array preserve numeric key or not. It has boolean value 'true' OR 'false'. If it is set to true, then it preserves the numeric key.
Example
<?php
$arr = array(10, 11, 35, 12, 44);
$arr_revs = array_reverse($arr);
print_r($arr_revs);
?>
Output :
Array ( [0] => 44 [1] => 12 [2] => 35 [3] => 11 [4] => 10 )If you set the preserve_key to 'TRUE'
<?php
$arr = array(10, 11, 35, 12, 44);
$arr_revs = array_reverse($arr, true);
print_r($arr_revs);
?>
Output :
Array ( [4] => 44 [3] => 12 [2] => 35 [1] => 11 [0] => 10 )The preserve key parameter does not effect on non numeric key.
Example
<?php
$arr = array(a=>10, b=>11, c=>35, f=>12, r=>44);
$arr_revs = array_reverse($arr);
Print_r($arr_revs);
echo '<br/>';
$arr_revs = array_reverse($arr, true);
Print_r($arr_revs);
?>
Output :
Array ( [r] => 44 [f] => 12 [c] => 35 [b] => 11 [a] => 10 )Array ( [r] => 44 [f] => 12 [c] => 35 [b] => 11 [a] => 10 )
Both results are same
Related PHP Functions
PHP array_reverse() functionPHP array_diff() function
PHP array_key_exists() function
PHP array_push() function
PHP array_search() function
PHP class_exists() function
PHP curl_setopt() function
PHP die() function
PHP dirname() function
PHP each() function
PHP explode() function
PHP file_exists() function
PHP function_exists() function
PHP getenv() function
PHP is_readable() function
PHP ksort() function
PHP mkdir() function
PHP ob_start() function
PHP parse_url() function
PHP str_repeat() function
PHP substr() function