Developers often find it useful to use this data structure in PHP to sort values or array elements. PHP provides some sorting functions suitable for multiple arrays. These functions allow you to arrange elements within the array, and allow you to re-Sort them in many different ways. In this article, we will discuss the most important functions of this sort.
Simple sorting
First, let's take a look at the simplest situation: simply sorting an array element from low to high. This function can be arranged in numbers or in alphabetical order. PHP's sort () function implements this function, as shown in Listing:
Listing
Copy codeThe Code is as follows:
<? Php
$ Data = array (5, 8, 1, 7, 2 );
Sort ($ data );
Print_r ($ data );
?>
The output result is as follows:
Copy codeThe Code is as follows:
Array ([0] => 1
[1] => 2
[2] => 5
[3] => 7
[4] => 8
)
You can also use the rsort () function for sorting. The result is the opposite to the previous simple sorting result of sort. The Rsort () function is used to reverse the array elements from high to low. It can also be arranged by number or in alphabetical order. Listing B shows an example of Listing:
Listing B
Copy codeThe Code is as follows:
<? Php $ data = array (5, 8, 1, 7, 2); rsort ($ data); print_r ($ data );
?>
The output result is as follows:
Copy codeThe Code is as follows:
Array ([0] => 8
[1] => 7
[2] => 5
[3] => 2
[4] => 1
)
Sort by keyword
When we use an array, we often sort the array again based on the keyword, from high to low. The Ksort () function is a sort function based on keywords. It also maintains the relevance of keywords during sorting. Listing C is an example:
Listing C
Copy codeThe Code is as follows:
<? Php $ data = array ("US" => "United States", "IN" => "India", "DE" => "Germany ", "ES" => "Spain"); ksort ($ data); print_r ($ data );
?>
The output result is as follows:
Copy codeThe Code is as follows:
Array ([DE] => Germany
[ES] => Spain
[IN] => India
[US] => United States
)
The Krsort () function is used to reverse the Array Based on the keyword. Listing D is an example of this:
Listing D
Copy codeThe Code is as follows:
<? Php $ data = array ("US" => "United States", "IN" => "India", "DE" => "Germany ", "ES" => "Spain"); krsort ($ data); print_r ($ data );
?>
The output result is as follows:
Copy codeThe Code is as follows:
Array ([US] => United States
[IN] => India
[ES] => Spain
[DE] => Germany
)
Sort by value
If you want to use value sorting to replace keyword sorting, PHP can also meet your requirements. You only need to use the asort () function to replace the previously mentioned ksort () function. As shown in Listing E:
Listing E
Copy codeThe Code is as follows:
<? Php $ data = array ("US" => "United States", "IN" => "India", "DE" => "Germany ", "ES" => "Spain"); asort ($ data); print_r ($ data );
?>
The output result is as follows. Note that this result is different from the result obtained by using the ksort () function. In both cases, the results are sorted alphabetically, however, they are sorted based on different fields of the array.
At the same time, note that the relationship between the key-value pairs is always maintained. It is only a method after the key-value pairs are sorted, and their correspondence is not changed.
Array ([DE] => Germany
[IN] => India
[ES] => Spain
[US] => United States
)
Now, you can certainly guess that this sort can also be inverted, which uses the arsort () function to complete this function. Listing F is an example:
Listing F
Copy codeThe Code is as follows:
<? Php $ data = array ("US" => "United States", "IN" => "India", "DE" => "Germany ", "ES" => "Spain"); arsort ($ data); print_r ($ data );
?>
The following is its output result, which is inverted in alphabetical order of values. Compare the following results with the results generated after inverted sorting using the krsort () function, so that you can easily understand the differences between the two.
Copy codeThe Code is as follows:
Array ([US] => United States
[ES] => Spain
[IN] => India
[DE] => Germany
)
Natural Language sorting
PHP has a very unique sorting method that uses cognition rather than computing rules. This feature is called natural language sorting. This sorting method is very useful when you create a fuzzy logic application. The following is a simple example, as shown in Listing G:
Listing G
Copy codeThe Code is as follows:
<? Php $ data = array ("book-1", "book-10", "book-100", "book-5"); sort ($ data ); print_r ($ data );
Natsort ($ data); print_r ($ data);?>
The output result is as follows:
Copy codeThe Code is as follows:
Array ([0] => book-1
[1] => book-10
[2] => book-100
[3] => book-5
)
Array
(
[0] => book-1
[3] => book-5
[1] => book-10
[2] => book-100
)
Their differences are clear: the second sorting result is more intuitive and "humanized". However, the first sorting result is more in line with algorithm rules and is more "computer.
Can natural languages be inverted? The answer is yes! You only need to use the array_reverse () function for the natsort () result. Listing H is a simple example:
Listing H
Copy codeThe Code is as follows:
<? Php $ data = array ("book-1", "book-10", "book-100", "book-5"); natsort ($ data ); print_r (array_reverse ($ data ));
?>
The output result is as follows:
Copy codeThe Code is as follows:
Array ([0] = & gt; book-100
[1] => book-10
[2] => book-5
[3] => book-1
)
Sort by custom rules
PHP also allows you to define your own sorting algorithm. you can create your own comparison function and pass it to the usort () function. If the first parameter is "smaller" than the second parameter, the comparison function must return a number smaller than 0. If the first parameter is "Greater" than the second parameter, the comparison function returns a number greater than 0.
Listing I is an example of sorting array elements based on their length. The shortest items are placed at the beginning:
Listing I
Copy codeThe Code is as follows:
<? Php $ data = array ("joe@host.com", "john.doe@gh.co.uk", "asmithsonian@us.info", "jay@zoo.tw"); usort ($ data, 'sortbylen ');
Print_r ($ data); function sortByLen ($ a, $ B ){
If (strlen ($ a) = strlen ($ B )){
Return 0;
} Else {
Return (strlen ($ a)> strlen ($ B ))? 1:-1;
}
}
?>
In this way, we have created our own comparison function. This function uses the strlen () function to compare the number of each string, and then returns 1, 0 or-1 respectively. the returned value is the basis for determining the arrangement of elements. The output result is as follows:
Copy codeThe Code is as follows:
Array ([0] => jay@zoo.tw
Joe@host.com
John.doe@gh.co.uk
Asmithsonian@us.info
)
Natural Language sorting
PHP has a very unique sorting method that uses cognition rather than computing rules. This feature is called natural language sorting. This sorting method is very useful when you create a fuzzy logic application. The following is a simple example, as shown in Listing G:
Listing G
Copy codeThe Code is as follows:
<? Php $ data = array ("book-1", "book-10", "book-100", "book-5"); sort ($ data ); print_r ($ data );
Natsort ($ data); print_r ($ data);?>
The output result is as follows:
Copy codeThe Code is as follows:
Array ([0] => book-1
[1] => book-10
[2] => book-100
[3] => book-5
)
Array
(
[0] => book-1
[3] => book-5
[1] => book-10
[2] => book-100
)
Their differences are clear: the second sorting result is more intuitive and "humanized". However, the first sorting result is more in line with algorithm rules and is more "computer.
Can natural languages be inverted? The answer is yes! You only need to use the array_reverse () function for the natsort () result. Listing H is a simple example:
Listing H
Copy codeThe Code is as follows:
<? Php $ data = array ("book-1", "book-10", "book-100", "book-5"); natsort ($ data ); print_r (array_reverse ($ data ));
?>
The output result is as follows:
Copy codeThe Code is as follows:
Array ([0] = & gt; book-100
[1] => book-10
[2] => book-5
[3] => book-1
)
Sort by custom rules
PHP also allows you to define your own sorting algorithm. you can create your own comparison function and pass it to the usort () function. If the first parameter is "smaller" than the second parameter, the comparison function must return a number smaller than 0. If the first parameter is "Greater" than the second parameter, the comparison function returns a number greater than 0.
Listing I is an example of sorting array elements based on their length. The shortest items are placed at the beginning:
Listing I
Copy codeThe Code is as follows:
<? Php $ data = array ("joe@host.com", "john.doe@gh.co.uk", "asmithsonian@us.info", "jay@zoo.tw"); usort ($ data, 'sortbylen ');
Print_r ($ data); function sortByLen ($ a, $ B ){
If (strlen ($ a) = strlen ($ B )){
Return 0;
} Else {
Return (strlen ($ a)> strlen ($ B ))? 1:-1;
}
}
?>
In this way, we have created our own comparison function. This function uses the strlen () function to compare the number of each string, and then returns 1, 0 or-1 respectively. the returned value is the basis for determining the arrangement of elements. The output result is as follows:
Copy codeThe Code is as follows:
Array ([0] => jay@zoo.tw
Joe@host.com
John.doe@gh.co.uk
Asmithsonian@us.info
)
Multi-dimensional sorting
Finally, PHP allows you to perform complex sorting on multi-dimensional arrays. For example, you can use a common keyword to sort a nested array and then sort it by another keyword. This is very similar to sorting multiple fields using the SQL ORDER BY statement. To better understand how it works, take a closer look at the example given by Listing J:
Listing J
Copy codeThe Code is as follows:
<? Php $ data = array ("id" => 1, "name" => "Boney M", "rating" => 3 ),
Array ("id" => 2, "name" => "Take That", "rating" => 1 ),
Array ("id" => 3, "name" => "The Killers", "rating" => 4 ),
Array ("id" => 4, "name" => "Lusain", "rating" => 3 ),
); Foreach ($ data as $ key => $ value ){
$ Name [$ key] = $ value ['name'];
$ Rating [$ key] = $ value ['rating'];
}
Array_multisort ($ rating, $ name, $ data); print_r ($ data);?>
Here, we simulate an array of rows and columns in the $ data array. Then, I use the array_multisort () function to rearrange the data set. First, sort the data set by rating, and then sort the data by name if rating is equal. The output result is as follows:
Copy codeThe Code is as follows:
Array ([0] => Array
(
[Id] => 2
[Name] => Take That
[Rating] => 1
) [1] => Array
(
[Id] => 1
[Name] => Boney M
[Rating] => 3
)
[2] => Array
(
[Id] => 4
[Name] => Lusain
[Rating] => 3
)
[3] => Array
(
[Id] => 3
[Name] => The Killers
[Rating] => 4
)
)
Array_multisort () is one of the most useful functions in PHP and has a wide range of applications. In addition, as you can see in the example, it can sort multiple irrelevant arrays, or use one of the elements as the basis for next sorting, you can also sort database result sets.
These examples should give you a preliminary understanding of the use of various array sorting functions in PHP, and also show you some internal functions hidden in the PHP Array Processing toolkit.