Home > Article > Backend Development > How to sort the values in an array by size in php
php method to sort several values in the array by size: 1. Sort the array elements in ascending order through the "sort($numbers);" function; 2. Use the "rsort($numbers);" function Just sort the array elements in descending order.
The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer
How to add several numbers in an array in php Values sorted by size?
Can be sorted through the sort() and rsort() functions.
Sort the array in ascending order - sort()
The following example sorts the elements in the array $numbers in ascending numerical order:
Example
<?php $numbers=array(3,5,1,22,11); sort($numbers); ?>
Output result:
1 3 5 11 22
Sort the array in descending order - rsort()
The following example sorts the elements in the array $numbers in descending numerical order:
Example
<?php $numbers=array(3,5,1,22,11); rsort($numbers); ?>
Output results:
22 11 5 3 1
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to sort the values in an array by size in php. For more information, please follow other related articles on the PHP Chinese website!