Home  >  Article  >  Backend Development  >  How to find the union of arrays in ascending and descending order in php

How to find the union of arrays in ascending and descending order in php

青灯夜游
青灯夜游Original
2022-05-26 20:39:111432browse

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)".

How to find the union of arrays in ascending and descending order in php

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);
?>

How to find the union of arrays in ascending and descending order in php

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);
?>

How to find the union of arrays in ascending and descending order in php

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);
?>

How to find the union of arrays in ascending and descending order in php

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!

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