If you have been using PHP for a while, you should be familiar with its array-this data structure allows you to store multiple values in a single variable, they can also be operated as a set.
If you have been using PHP for a while, you should be familiar with its array-this data structure allows you to store multiple values in a single variable, they can also be operated as a set.
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
The Code is as follows:
$ Data = array (5, 8, 1, 7, 2 );
Sort ($ data );
Print_r ($ data );
?>
The output result is as follows:
The 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
The Code is as follows:
?>
The output result is as follows:
The 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
The Code is as follows:
"United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain"); ksort ($ data ); print_r ($ data );
?>
The output result is as follows:
The 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
The Code is as follows:
"United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain"); krsort ($ data ); print_r ($ data );
?>
The output result is as follows:
The 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
The Code is as follows:
"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
The Code is as follows:
"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.
The 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
The Code is as follows:
Natsort ($ data); print_r ($ data);?>
The output result is as follows:
The 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
The Code is as follows:
?>
The output result is as follows:
The 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 in which array elements are sorted based on their length. The shortest items of the Hong Kong VM are placed at the beginning:
Listing I
The Code is as follows: