PHP array_reverse() function usage introduction
PHP is a widely used open source scripting language that can build dynamic websites and web applications by writing PHP code. PHP has many powerful functions that can help developers easily handle arrays, one of the important functions is array_reverse().
The array_reverse() function provides a simple and efficient method for reversing the order of the elements of an array and returning a new array. This function is usually used to output PHP arrays in reverse order, but it can also be used for other types of arrays.
Usage method
The syntax of the array_reverse() function is:
array array_reverse (array $array, bool $preserve_keys = FALSE)
Among them, $ The array parameter is required and represents the array to be reversed. The $preserve_keys parameter is optional. If it is true, the original array key name will be retained, otherwise the index will be re-established. Here is an example of this function:
$array = array('a', 'b', 'c');
$reverse1 = array_reverse($array); // By default, the $preserve_keys parameter is FALSE
print_r($reverse1);
$reverse2 = array_reverse($array, true); // The $preserve_keys parameter is TRUE
print_r($ reverse2);
?>
The output result is:
Array ( [0] => c [1] => b [2] => a )
Array ([2] => c [1] => b [0] => a )
In the above example, in the first example, the $ of array_reverse() function The preserve_keys parameter is FALSE, so the keys in the returned array will be renumbered. In the second example, the $preserve_keys parameter is TRUE, and the returned array will retain the original key names.
More examples
The above examples are just the basic operations used by the array_reverse() function. More practices can be carried out for different array types and usage scenarios. The following are more practical examples:
The array_reverse() function can also be used to process multi-dimensional arrays, just process each sub-array as a separate array, as follows:
$multi_array = array(
70aea06dcd7a7ac3b6d9b3f69908ce42}
print_r($reverse_multi);
?>
The output result is:
Array ( [0] => ; Array ( [0] => c [1] => b [2] => a ) [1] => Array ( [0] => f [1] => e [2] => d ) [2] => Array ( [0] => i [1] => h [2] => g ) )
Another common usage scenario is to output the characters in a single string in reverse order. You can first convert the string to an array, then use the array_reverse() function to process the array, and finally use the implode() function to convert the array back to string form, as follows:
$str = 'Hello world';
$reverse_str = implode('',array_reverse(str_split($str)));
echo $reverse_str; // Output dlrow olleH
?>
The array_reverse() function can also be used to arrange associative arrays in reverse order. You only need to pay attention to retaining the original key-value pairs, as shown below:
$array = array('one' => 1, 'two' => 2, 'three' => 3);
$reverse = array_reverse ($array, true);
print_r($reverse);
?>
The output result is:
Array ( [three] => 3 [two] => 2 [one] => 1 )
Summary
array_reverse() is a very practical array function in PHP, used to reverse the order of elements in an array. Allows developers to easily sort arrays in reverse order and retain or delete original key names as needed. This article provides how to use the array_reverse() function and some examples, hoping to help PHP developers.
The above is the detailed content of Introduction to the use of PHP array_reverse() function. For more information, please follow other related articles on the PHP Chinese website!