Home >Backend Development >PHP Tutorial >php array_reverse returns an array in reverse order detailed explanation

php array_reverse returns an array in reverse order detailed explanation

墨辰丷
墨辰丷Original
2018-05-24 09:36:411502browse

This article mainly introduces the code of php array_reverse to return array instances in reverse order. It has certain reference value. Those who are interested can learn about it.

php The array_reverse function returns an array with the units in reverse order. This function has two parameters. The first parameter indicates the array to be processed. The second parameter is optional and specifies whether to retain the key names of the original array. This article explains how to use the array_reverse function through examples. Coders who need it can refer to it.

Basic syntax of the array_reverse function:

array array_reverse ( array $array [, bool $preserve_keys = false ] )

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

If the second parameter is specified as true, the key name of the element remains unchanged, otherwise the key name will be lost.

Parameter introduction


##ParameterDescriptionarrayRequired. The input array. preserve
Optional. Specifies whether to retain the original array key names. Returns an array with cells in reverse order. If set to TRUE numeric keys will be retained. Non-numeric keys are not affected by this setting and will always be retained.

Possible values:

  • true

  • false

Return value

Returns the reversed array.

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 note the key name difference. The printouts of $result and $result_keyed are respectively:

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 entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHPMethods to dynamically compress js and css files

PHP PDO transaction processing operation example

PHPSimple implementation of parsing xml into an array method

The above is the detailed content of php array_reverse returns an array in reverse order detailed explanation. 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