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

黄舟
Release: 2023-03-17 08:26:01
Original
1910 people have browsed it

Example

Return the array in reversed order:

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

Definition and usage

array_reverse() FunctionReturn the flip Sequential array.

Syntax

array_reverse(array,preserve)
Copy after login
ParametersDescription
array Required. Specifies an array.
preserveOptional. 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);
?>
Copy after login

Example:

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

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
)
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!