Home  >  Article  >  Backend Development  >  How to sort the values ​​in an array by size in php

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

藏色散人
藏色散人Original
2023-03-06 09:24:494611browse

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

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!

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