Home  >  Article  >  Backend Development  >  How to sort an array in php

How to sort an array in php

PHPz
PHPzOriginal
2023-04-14 19:01:18158browse

PHP is a popular programming language used for website development and application development. Regarding the various features of PHP, array sorting is probably one of the most important features when developing in PHP because they enable us to perform sorting operations on data in an array. This article will explore several ways to sort arrays in PHP.

  1. sort() function

The sort() function is one of the most commonly used sorting functions in PHP. This function sorts the array in ascending order. Its parameters are The array to be sorted, it can modify the array in place and return a Boolean value indicating whether the sorting is successful.

The syntax is as follows:

<code>bool sort ( array &$array , int $sort_flags = SORT_REGULAR )</code>

Among them, $array is the array that needs to be sorted, $sort_flags is an optional parameter, used to specify the sorting algorithm, and its default value is SORT_REGULAR.

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

<code>$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo $key . " = " . $val . "\n";
}</code>

The output result is:

<code>0 = apple
1 = banana
2 = lemon
3 = orange</code>
  1. rsort() function

rsort The () function is also one of the most commonly used sorting functions in PHP. The difference between this function and the sort() function is that it sorts the array in descending order. Its parameters are the same as the sort() function, including the array to be sorted and the optional $sort_flags parameter.

The syntax is as follows:

<code>bool rsort ( array &$array , int $sort_flags = SORT_REGULAR )</code>

The following is an example of the rsort() function:

<code>$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
foreach ($fruits as $key => $val) {
    echo $key . " = " . $val . "\n";
}</code>

The output result is:

<code>0 = orange
1 = lemon
2 = banana
3 = apple</code>
  1. asort() Function

asort() function can sort the array in ascending order according to its value. This function does not allow re-indexing of the array and modifies the array in place. Its parameters are the same as the sort() function.

The syntax is as follows:

<code>bool asort ( array &$array , int $sort_flags = SORT_REGULAR )</code>

The following is an example of the asort() function:

<code>$age = array("Tom"=>"35", "John"=>"25", "Lisa"=>"30");
asort($age);
foreach($age as $key => $val) {
    echo "$key = $val\n";
}</code>

The output result is:

<code>John = 25
Lisa = 30
Tom = 35</code>
  1. arsort() function

The arsort() function is the reverse sorting version of the asort() function, which sorts by value in descending order. Its parameters are the same as other sorting functions.

The syntax is as follows:

<code>bool arsort ( array &$array , int $sort_flags = SORT_REGULAR )</code>

The following is an example of the arsort() function:

<code>$age = array("Tom"=>"35", "John"=>"25", "Lisa"=>"30");
arsort($age);
foreach($age as $key => $val) {
    echo "$key = $val\n";
}</code>

The output result is:

<code>Tom = 35
Lisa = 30
John = 25</code>
  1. ksort() function

ksort() function can sort the array by key (key name). It will sort the key values ​​in the array in ascending order, does not allow re-indexing of the array, and modifies the array in place. Its parameters are the same as other sorting functions.

The syntax is as follows:

<code>bool ksort ( array &$array , int $sort_flags = SORT_REGULAR )</code>

The following is an example of the ksort() function:

<code>$age = array("Tom"=>"35", "John"=>"25", "Lisa"=>"30");
ksort($age);
foreach($age as $key => $val) {
    echo "$key = $val\n";
}</code>

The output result is:

<code>John = 25
Lisa = 30
Tom = 35</code>
  1. krsort() function

The krsort() function is the reverse sorting version of the ksort() function. This function sorts the array by key (key name) in descending order, does not allow re-indexing of the array, and modifies the array in place. Its parameters are the same as other sorting functions.

The syntax is as follows:

<code>bool krsort ( array &$array , int $sort_flags = SORT_REGULAR )</code>

The following is an example of the krsort() function:

<code>$age = array("Tom"=>"35", "John"=>"25", "Lisa"=>"30");
krsort($age);
foreach($age as $key => $val) {
    echo "$key = $val\n";
}</code>

The output result is:

<code>Tom = 35
Lisa = 30
John = 25</code>
  1. usort() function

usort() function allows us to customize the sorting of arrays. It requires us to provide a custom comparison function for comparison. This function does not allow re-indexing of the array and modifies the array in place. This function requires two parameters. The first parameter is the array to be sorted, and the second parameter is a custom comparison function used for sorting.

The syntax is as follows:

<code>bool usort ( array &$array , callable $value_compare_func )</code>

The following is an example of the usort() function:

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

$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");

foreach ($a as $key => $val) {
    echo "$key: $val\n";
}</code>

The output result is:

<code>0: 1
1: 2
2: 3
3: 5
4: 6</code>

The above are the seven types in PHP Commonly used array sorting functions. They can sort an array into ascending or descending order based on specified criteria. Choose different sorting functions according to development needs to achieve the desired array sorting effect.

The above is the detailed content of How to sort an array 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
Previous article:php array output methodNext article:php array output method