Detailed explanation of array sorting method of PHP function operation

WBOY
Release: 2023-06-19 22:58:02
Original
3600 people have browsed it

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:

  1. sort(): Sort the array in ascending order
  2. rsort(): Sort the array in descending order
  3. asort(): Sort the array in ascending order and retain the key-value relationship
  4. ksort(): Sort the array by key name Sort in ascending order
  5. arsort(): Sort the array in descending order and preserve the key-value relationship
  6. krsort(): Sort the array in descending order by key name
  7. usort(): Sort the array using a user-defined comparison function
  8. uasort(): Sort the array using a user-defined comparison function and preserve the key-value relationship
  9. uksort(): Sort the array Use user-defined comparison function to sort by key name

2. Usage examples

Below we use several examples to demonstrate how to use the sorting function in PHP.

  1. sort() and rsort()

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 )
?>
Copy after login
  1. asort() and arsort()

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 )
?>
Copy after login
  1. ksort() and krsort()

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 )
?>
Copy after login
  1. usort(), uasort() and uksort()

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

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!

Related labels:
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