Home  >  Article  >  Backend Development  >  How to sort by array value in php

How to sort by array value in php

PHPz
PHPzOriginal
2023-04-26 09:08:282801browse

In PHP, we can sort an array in ascending and descending order using the built-in functions sort() and rsort(). However, these two functions can only sort the keys of the array by key value. If you want to sort the values ​​in the array by key value, you need to use other functions.

The following introduces several methods of sorting by array values:

1. Use asort() and arsort()

In PHP, we can use the built-in function asort() to sort the array in ascending order by value, and use arsort() to sort the array in descending order by value. These two functions also preserve the association between key values.

$numbers = array(3, 1, 5, 2, 4);
asort($numbers);
print_r($numbers);
// Output: Array ( [1] => 1 [3] => 2 [0] => 3 [4] => 4 [2] => 5 )

arsort($numbers);
print_r($numbers);
// Output: Array ( [2] => 5 [4] => 4 [0] => 3 [3] => 2 [1] => 1 )

Note that if we use sort() and rsort() to sort the array by key value, the association between the key value and the value will be lost . asort() and arsort() can preserve the association between key values ​​and values.

2. Use usort()

PHP also provides the usort() function, which can sort arrays according to custom comparison functions . The comparison function needs to accept two parameters. If the first parameter is less than the second parameter, the function returns a negative value; if the two parameters are equal, the function returns 0; if the first parameter is greater than the second parameter, the function returns a positive value.

The following is an example of using usort() to sort an array in ascending order by value:

$numbers = array(3, 1, 5, 2, 4);

function compare_function($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

usort($numbers, &#39;compare_function&#39;);
print_r($numbers);
// Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

3. Use array_multisort()

Another common way is to use the array_multisort() function, which can sort multiple arrays at the same time. We can pass in an array and a sorting method, and then the function will sort the array values ​​according to the sorting method.

The following is an example of using the array_multisort() function to sort an array in ascending order:

$numbers = array(3, 1, 5, 2, 4);
array_multisort($numbers, SORT_ASC);
print_r($numbers);
// Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

Note that the array_multisort() function sorts all The arrays are sorted. If you need to sort by a specific array, you need to put this array in the first parameter.

Summary

There are many ways to implement sorting by array values ​​in PHP, and the above are just a few of them. When using these functions, you need to pay attention to the type and position of the parameters, as well as the choice of sorting method. At the same time, when there are multiple parameters that need to be sorted, using array_multisort() can make processing more convenient.

The above is the detailed content of How to sort by array value 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