What are the methods of sorting arrays in php

PHPz
Release: 2023-04-20 14:24:44
Original
3436 people have browsed it

In PHP, array is a very important data type, which can store a series of data and can be easily operated. When we need to sort an array, PHP provides many built-in functions and methods.

This article will introduce the commonly used array sorting methods in PHP, mainly including the following:

  1. array_sort() function
  2. sort() function
  3. asort() function
  4. ksort() function
  5. usort() function
  6. uasort() function
  7. uksort() function
  8. natsort() function
  9. arsort() function
  10. rsort() function
  11. array_sort() function

array_sort() function It is a custom function in PHP that can be used to sort arrays. When calling this function, you need to pass in two parameters. The first parameter is the array to be sorted, and the second parameter is the sorting method. You can use SORT_ASC and SORT_DESC.

The following is an example of using the array_sort() function to sort an array:

function array_sort($arr,$keys,$type=SORT_ASC){
    $keysvalue = array();
    foreach($arr as $key=>$val){
        $keysvalue[$key] = $val[$keys];
    }
    if($type == SORT_ASC){
        asort($keysvalue);
    }else{
        arsort($keysvalue);
    }
    reset($keysvalue);
    foreach($keysvalue as $key=>$vals) {
        $keysort[$key] = $key;
    }
    $new_array = array();
    foreach($keysort as $key=>$val) {
        $new_array[$val] = $arr[$val];
    }
    return $new_array;
}
Copy after login

Usage method:

$arr ​​= array(

array('id'=>1,'name'=>'Tom','age'=>20),
array('id'=>2,'name'=>'Jerry','age'=>18),
array('id'=>3,'name'=>'Lucy','age'=>22)
Copy after login

) ;
$arr ​​= array_sort($arr,'age',SORT_DESC);
print_r($arr);

Output result:

Array
(

[0] => Array
    (
        [id] => 3
        [name] => Lucy
        [age] => 22
    )

[1] => Array
    (
        [id] => 1
        [name] => Tom
        [age] => 20
    )

[2] => Array
    (
        [id] => 2
        [name] => Jerry
        [age] => 18
    )
Copy after login

)

  1. sort() function

The sort() function is one of the commonly used sorting functions in PHP, mainly for sorting arrays. It can be sorted in ascending or descending order, or it can keep the index relationship of the array unchanged.

The following is an example of using the sort() function to sort an array:

$arr ​​= array(3,1,4,2,5);
sort($arr );
print_r($arr);

Output result:

Array
(

[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
Copy after login

)

  1. asort( )Function

asort() function is a method of sorting an array. It can be sorted in ascending or descending order, but the index relationship of the array will remain unchanged. When using the asort() function, it sorts the array based on its values.

The following is an example of using the asort() function to sort an array:

$arr ​​= array(3,1,4,2,5);
asort($arr );
print_r($arr);

Output result:

Array
(

[1] => 1
[3] => 2
[0] => 3
[2] => 4
[4] => 5
Copy after login

)

  1. ksort( )Function

ksort() function is a method of sorting an array. It can be sorted in ascending or descending order, but it will maintain the key and value of the array. The relationship between them remains unchanged.

The following is an example of using the ksort() function to sort an array:

$arr ​​= array('d'=>'Dog','a'=>'Apple ','c'=>'Cat','b'=>'Boy');
ksort($arr);
print_r($arr);

Output result:

Array
(

[a] => Apple
[b] => Boy
[c] => Cat
[d] => Dog
Copy after login
Copy after login
Copy after login

)

  1. usort() function

usort() function is to sort the array A method that is different from other sorting functions, the usort() function allows you to customize a sorting algorithm. When using the usort() function, you can specify a comparison function that will compare the values ​​of the array based on a desired criterion (such as the number of characters contained in the string or the size of the number).

The following is an example of using the usort() function to sort an array:

function cmp($a, $b)
{

if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
Copy after login
Copy after login
Copy after login

}
$arr ​​= array(3,1,4,2,5);
usort($arr, "cmp");
print_r($arr);

Output result:

Array
(

[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
Copy after login

)

  1. uasort() function

uasort() function is a sort in PHP function, which is similar to the usort() function and allows you to customize the sorting algorithm. But unlike the usort() function, it keeps the key values ​​of the array unchanged.

The following is an example of using the uasort() function to sort an array:

function cmp($a, $b)
{

if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
Copy after login
Copy after login
Copy after login

}
$arr ​​= array('d'=>'Dog','a'=>'Apple','c'=>'Cat','b'=>'Boy');
uasort($arr, "cmp");
print_r($arr);

Output result:

Array
(

[a] => Apple
[b] => Boy
[c] => Cat
[d] => Dog
Copy after login
Copy after login
Copy after login

)

  1. uksort() function

uksort() function is a sorting function in PHP. It is similar to ksort() function and can also be sorted in ascending or descending order. . But unlike the ksort() function, it allows you to customize the sorting algorithm and keeps the relationship between keys and values ​​unchanged.

The following is an example of using the uksort() function to sort an array:

function cmp($a, $b)
{

if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
Copy after login
Copy after login
Copy after login

}
$arr ​​= array('d'=>'Dog','a'=>'Apple','c'=>'Cat','b'=>'Boy');
uksort($arr, "cmp");
print_r($arr);

Output result:

Array
(

[a] => Apple
[b] => Boy
[c] => Cat
[d] => Dog
Copy after login
Copy after login
Copy after login

)

  1. natsort() function

The natsort() function is a sorting function in PHP. It sorts an array according to natural numbers. This may not conform to human intuition, because It compares the numbers in the string.

The following is an example of using the natsort() function to sort an array:

$arr ​​= array('img12.png', 'img10.png', 'img2.png', 'img1.png');
natsort($arr);
print_r($arr);

Output result:

Array
(

[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
Copy after login

)

  1. arsort()函数

arsort()函数是PHP中的一种排序函数,它可以按照降序方式将数组中的元素进行排序。该函数不会保留原来数组中的键(key)与值(value)之间的关系。

下面是一个使用arsort()函数对数组进行排序的例子:

$arr = array(3,1,4,2,5);
arsort($arr);
print_r($arr);

输出结果:

Array
(

[4] => 5
[2] => 4
[0] => 3
[1] => 1
[3] => 2
Copy after login

)

  1. rsort()函数

rsort()函数是PHP中常用的排序函数之一,它可以按照降序的方式来排序。该函数不会保留原来数组中的键(key)与值(value)之间的关系。

下面是一个使用rsort()函数对数组进行排序的例子:

$arr = array(3,1,4,2,5);
rsort($arr);
print_r($arr);

输出结果:

Array
(

[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
Copy after login

)

总结:

以上介绍了PHP中常用的数组排序方法,包括array_sort()函数、sort()函数、asort()函数、ksort()函数、usort()函数、uasort()函数、uksort()函数、natsort()函数、arsort()函数和rsort()函数。不同的排序函数有不同的需求和使用场景,开发者应根据具体的需求来选择合适的排序函数。

The above is the detailed content of What are the methods of sorting arrays in php. For more information, please follow other related articles on the PHP Chinese website!

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