How to sort the values ​​in an array by size in php

藏色散人
Release: 2023-03-17 22:54:01
Original
4543 people have browsed it

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.

How to sort the values ​​in an array by size in php

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);
?>
Copy after login

Output result:

1
3
5
11
22
Copy after login

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);
?>
Copy after login

Output results:

22
11
5
3
1
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!