With the continuous updating and improvement of Internet technology, PHP has gradually become one of the most popular programming languages in the field of Web development. As a powerful language, PHP's function library is becoming more and more abundant. Whether you are a beginner or an experienced PHP developer, it is inevitable to understand the various function operations in PHP. This article will focus on how to sort arrays in PHP.
1. Common sorting methods
PHP provides developers with a variety of sorting methods, including:
2. Usage examples
Below we use several examples to demonstrate how to use the sorting function in PHP.
The sort() function is used to sort the array in ascending order, and the rsort() function is used to sort the array in descending order. Here is a simple demonstration example:
<?php $arr = array(3, 1, 2, 5, 4); sort($arr); print_r($arr); //输出结果为:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) rsort($arr); print_r($arr); //输出结果为:Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 ) ?>
asort() function is used to sort the array in ascending order and retain the key values Relationship, the arsort() function is used to sort the array in descending order and preserve the key-value relationship. The following is a simple demonstration example:
<?php $arr = array("a" => 3, "b" => 1, "c" => 2, "d" => 5, "e" => 4); asort($arr); print_r($arr); //输出结果为:Array ( [b] => 1 [c] => 2 [a] => 3 [e] => 4 [d] => 5 ) arsort($arr); print_r($arr); //输出结果为:Array ( [d] => 5 [e] => 4 [a] => 3 [c] => 2 [b] => 1 ) ?>
The ksort() function is used to sort the array in ascending order by key name, krsort The () function is used to sort the array in descending order by key name. The following is a simple demonstration example:
<?php $arr = array("b" => 1, "c" => 2, "a" => 3); ksort($arr); print_r($arr); //输出结果为:Array ( [a] => 3 [b] => 1 [c] => 2 ) krsort($arr); print_r($arr); //输出结果为:Array ( [c] => 2 [b] => 1 [a] => 3 ) ?>
usort() function, uasort() function and uksort( ) function is usually used to sort custom sorting rules. The following is a simple demonstration example:
<?php $arr = array( array("name" => "Tom", "age" => 20), array("name" => "Jerry", "age" => 18), array("name" => "Bob", "age" => 23), ); //按照年龄升序排序 function cmp($a, $b) { if ($a["age"] == $b["age"]) { return 0; } else { return ($a["age"] < $b["age"]) ? -1 : 1; } } usort($arr, "cmp"); print_r($arr); //输出结果为:Array ( [0] => Array ( [name] => Jerry [age] => 18 ) [1] => Array ( [name] => Tom [age] => 20 ) [2] => Array ( [name] => Bob [age] => 23 ) ) //按照年龄降序排序,并保留键值关系 function cmp1($a, $b) { if ($a["age"] == $b["age"]) { return 0; } else { return ($a["age"] > $b["age"]) ? -1 : 1; } } uasort($arr, "cmp1"); print_r($arr); //输出结果为:Array ( [1] => Array ( [name] => Tom [age] => 20 ) [2] => Array ( [name] => Bob [age] => 23 ) [0] => Array ( [name] => Jerry [age] => 18 ) ) //按照姓名首字母升序排序 function cmp2($a, $b) { return strcmp($a["name"], $b["name"]); } uksort($arr, "cmp2"); print_r($arr); //输出结果为:Array ( [2] => Array ( [name] => Bob [age] => 23 ) [1] => Array ( [name] => Jerry [age] => 18 ) [0] => Array ( [name] => Tom [age] => 20 ) ) ?>
3. Summary
Through the introduction of this article, we have learned about the use of various array sorting methods in PHP. Whether you are a beginner or an experienced developer, these sorting methods are very important basic knowledge and are often used in daily development. Therefore, mastering these methods will help you quickly develop efficient and high-quality web applications.
The above is the detailed content of Detailed explanation of array sorting method of PHP function operation. For more information, please follow other related articles on the PHP Chinese website!