Home > Article > Backend Development > How to find the union of arrays in ascending and descending order in php
Method: 1. Use "sort (array)" to sort in ascending order; 2. Use "rsort (array)" to sort in descending order; 3. Use array_merge_recursive() to merge ascending arrays and descending arrays, the syntax "array_merge_recursive" (Ascending array, Descending array)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php ascends the array Method of finding the union after descending order
Step 1: Use the sort() function to sort the array in ascending order
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10, 23, 5, 12, 84, 16); var_dump($arr); sort($arr); $arr1=$arr; var_dump($arr1); ?>
Step 2: Use rsort() function to sort the array in descending order
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10, 23, 5, 12, 84, 16); var_dump($arr); rsort($arr); $arr2=$arr; var_dump($arr2); ?>
Step 3: Compare ascending array and descending order The key name and key value of the array, get the union
Use the array_merge_recursive() function to merge the ascending sorted array and the descending sorted array
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10, 23, 5, 12, 84, 16); sort($arr); $arr1=$arr; rsort($arr); $arr2=$arr; $c = array_merge_recursive($arr1,$arr2); var_dump($c); ?>
Recommended learning: "PHP video tutorial"
The above is the detailed content of How to find the union of arrays in ascending and descending order in php. For more information, please follow other related articles on the PHP Chinese website!