Merge and sort steps: 1. Use the array_merge() function to merge two arrays. The syntax "array_merge(array 1, array 2)" will return a merged array; 2. Use asort() Or the sort() function sorts the merged array in ascending order. The elements will be sorted from small to large. The syntax is "sort (merge array)" or "asort (merge array)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Two arrays are known
$a1=array(1,3,5,7,9); $a2=array(2,4,6,8,10);
How to merge two arrays and sort them from small to large in PHP?
In PHP, you can use the array_merge() function and asort() (or sort()) function to merge two arrays and sort them from small to large.
Implementation steps:
Step 1: Use the array_merge() function to merge two arrays
array_merge() function Used to combine one or more arrays into a single array. If two or more array elements have the same key name, the last element overwrites the others.
<?php header('content-type:text/html;charset=utf-8'); $a1=array(1,3,5,7,9); $a2=array(2,4,6,8,10); var_dump($a1); var_dump($a2); echo "合并数组:"; $arr=array_merge($a1,$a2); var_dump($arr); ?>
Step 2: Use the asort() or sort() function to sort the merged array in ascending order - sort the elements from small to large
sort() - Sort the array in ascending order
asort() - Sort the array in ascending order based on the values of the associative array
echo "合并数组升序排序后:"; sort($arr); var_dump($arr);
echo "合并数组升序排序后:"; asort($arr); var_dump($arr);
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to merge two arrays in php and sort them from small to large. For more information, please follow other related articles on the PHP Chinese website!