Home  >  Article  >  Backend Development  >  A direct look at the PHP array_reverse() function principle and example analysis

A direct look at the PHP array_reverse() function principle and example analysis

coldplay.xixi
coldplay.xixiforward
2020-07-22 16:37:092412browse

A direct look at the PHP array_reverse() function principle and example analysis

##array_reverse
##(PHP 4, PHP 5, PHP 7)

array_reverse

—Returns an array with the cells in reverse order

Description


##array_reverse(array$array[,bool$preserve_keys= FALSE] ) :array

array_reverse() accepts an array array as input and returns a new array with the cells in reverse order.

Parameters


array

Input array

preserve_keys

If set to TRUE, numeric keys will be retained. Non-numeric keys are not affected by this setting and will always be retained.

Return value


Returns the reversed array.

Example

##Example #1array_reverse() example

`<?php
$input = array("php", 4.0, array("green", "red"));
$reversed = array_reverse($input);
$preserved = array_reverse($input, true);

print_r($input);
print_r($reversed);
print_r($preserved);
?>`
The above routine will output:

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

)
Array
(
  [0] => Array
    (
      [0] => green
      [1] => red
    )

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

  [1] => 4
  [0] => php
)

Related learning recommendations:

PHP programming from entry to proficiency

The above is the detailed content of A direct look at the PHP array_reverse() function principle and example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete