Home  >  Article  >  Backend Development  >  php function array_reverse() that returns an array in reverse order

php function array_reverse() that returns an array in reverse order

黄舟
黄舟Original
2017-11-08 13:47:401915browse

Example

Return the array in reversed order:

<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
?>

Definition and usage

array_reverse() FunctionReturn the flip Sequential array.

Syntax

array_reverse(array,preserve)
Parameters Description
array Required. Specifies an array.
preserve Optional. Specifies whether to retain the original array key names.
If set to TRUE, numeric keys will be preserved. Non-numeric keys are not affected by this setting and will always be retained.
Possible values:
  • true

  • false

Technical details

The preserve parameter is
Return value: Returns the flipped array.
PHP Version: 4+
##Update Log : new in PHP 4.0.3.
More examples

Example 1

Output the original array, flipped array, and flipped array retaining the original array key names:

<?php
$a=array("Volvo","XC90",array("BMW","Toyota"));
$reverse=array_reverse($a);
$preserve=array_reverse($a,true); 
print_r($a);print_r($reverse);
print_r($preserve);
?>

Example:

<?php
$input = array("php", 4.0, array("green", "red"));
$result = array_reverse($input);
$result_keyed = array_reverse($input, true);
?>

This will make $result and $result_keyed have the same unit, but pay attention to the difference in key names. The printout of $result and $result_keyed respectively shows:

Array
(
 [0] => Array
 (
  [0] => green
  [1] => red
 )
 
 [1] => 4
 [2] => php
)
Array
(
 [2] => Array
 (
  [0] => green
  [1] => red
 )
 
 [1] => 4
 [0] => php
)


The above is the detailed content of php function array_reverse() that returns an array in reverse order. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn