Often, developers find it useful to sort values or array elements using this data structure in PHP. PHP provides some sorting functions for a variety of arrays. These functions allow you to arrange elements within the array and also allow you to reorder them in many different ways. In this article we will discuss some of the most important functions in this sorting.
Simple sorting
First, let's take a look at the simplest case: simply sorting an array element from low to high. This function can sort either numerically or alphabetically. PHP's sort() function implements this function, as shown in Listing A:
Listing A
Copy code The code is as follows:
$data = array(5,8,1,7,2);
sort($data);
print_r($data);
?> ;
The output result is as follows:
Copy the code 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, and its result is opposite to the simple sorting result of sort() used previously. The Rsort() function sorts the array elements from high to low, either numerically or alphabetically. Listing B shows us an example of it:
Listing B
Copy code The code is as follows:
< ?php $data = array(5,8,1,7,2);rsort($data); print_r($data);
?>
The output result is as follows :
Copy code The code is as follows:
Array ([0] => 8
[1] => ; 7
[2] => 5
[3] => 2
[4] => 1
)
Sort by keyword
When we use arrays, we often reorder the array according to keywords, from high to low. The Ksort() function is a function that sorts according to keywords. At the same time, it maintains the relevance of keywords during the sorting process. Listing C is an example:
Listing C
Copy code The code is as follows:
“United States”, “IN” => “India”, “DE” => “Germany”, “ES” => “Spain”);ksort($data); print_r($data);
?>
Its output is as follows:
Copy code The code is as follows:
Array ([DE] => Germany
[ES] => Spain
[IN] => India
[US] => United States
)
The Krsort() function inverts the array based on keywords. Listing D is an example of this:
Listing D
Copy the code The code is as follows:
“United States”, “IN” => “India”, "DE" => "Germany", "ES" => "Spain"); krsort($data); print_r($data);
?>
its The output result is as follows:
Copy code The code is as follows:
Array ([US] => United States
[ IN] => India
[ES] => Spain
[DE] => Germany
)
Sort by value
if you want to use value If sorting is used instead of keyword sorting, PHP can also meet your requirements. You just need to use the asort() function instead of the ksort() function mentioned earlier. As shown in Listing E:
Listing E
Copy code The code is as follows:
“United States”, “IN” => “India”, “DE” => “Germany”, “ES” => “Spain”);asort($data); print_r($data);
?>
The following is its output. Note the difference between this result and the result obtained using the ksort() function above - in both cases, the sorting is done alphabetically, but they are sorted according to different fields of the array.
At the same time, please note that the relationship between keywords and values will always be maintained; it is just a way to sort the keyword-value pairs, and the sorting will not change their correspondence.
Array ([DE] => Germany
[IN] => India
[ES] => Spain
[US] => United States
)
Now, as you can no doubt guess, this sort can also be inverted, which uses the arsort() function to accomplish this. Listing F is an example:
Listing F
Copy code The code is as follows:
“United States”, “IN” => “India”, “DE” => “Germany”, “ES” => “Spain”);arsort($data); print_r($data);
?>
The following is its output, sorted alphabetically according to the value. Comparing the following results with the results generated after inversion using the krsort() function, you can easily understand the difference between the two.
Copy code The code is as follows:
Array ([US] => United States
[ES] => ; Spain
[IN] => India
[DE] => Germany
)
Natural language sorting
PHP has a very unique way of sorting, This approach uses cognition rather than computational rules. This feature is called natural language ranking, and it is very useful when creating fuzzy logic applications. Below you can take a look at a simple example of it, as shown in Listing G:
Listing G
Copy code The code is as follows:
natsort($data); print_r($data);?>
Its output is as follows:
Copy code 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
)
The difference is already clear: the second sorting result is more Intuitive and more "human", the first one is more in line with algorithmic rules and has more "computer" characteristics.
Can natural language perform inversion? The answer is yes! Just use the array_reverse() function on the result of natsort(). Listing H is a simple example:
Listing H
Copy code The code is as follows :
?>
The following is its output:
Copy code The code is as follows:
Array ([0] => book-100
[1] => book-10
[2] => book-5
[3] => book-1
)
Sort according to user-defined 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 "larger" than the second parameter, the comparison function should return a number larger than 0. . An example of this is
Listing I, where array elements are sorted according to their length, with the shortest items first:
Listing I
Copy the code The code is as follows:
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 create 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. This return value is the basis for determining the arrangement of elements. The following is its output:
Copy code The code is as follows:
Array ([0] => jay@zoo .tw
[1] => joe@host.com
[2] => john.doe@gh.co.uk
[3] => asmithsonian@us.info
)
Natural language sorting
PHP has a very unique way of sorting, which uses cognition rather than computational rules. This feature is called natural language ranking, and it is very useful when creating fuzzy logic applications. Below you can take a look at a simple example of it, as shown in Listing G:
Listing G
Copy code The code is as follows:
natsort($data); print_r($data);?>
Its output is as follows:
Copy code 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
)
The difference is already clear: the second sorting result is more Intuitive and more "human", the first one is more in line with algorithmic rules and has more "computer" characteristics.
Can natural language perform inversion? The answer is yes! Just use the array_reverse() function on the result of natsort(). Listing H is a simple example:
Listing H
Copy code The code is as follows :
?>
The following is its output:
Copy code The code is as follows:
Array ([0] => book-100
[1] => book-10
[2] => book-5
[3] => book-1
)
Sort according to user-defined 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 "larger" than the second parameter, the comparison function should return a number larger than 0. . An example of this is
Listing I, where array elements are sorted according to their length, with the shortest items first:
Listing I
Copy the code The code is as follows:
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 create our own comparison function, which uses the strlen() function to compare the number of each string, Then return 1, 0 or -1 respectively. This return value is the basis for determining the arrangement of elements. The following is its output:
Copy code The code is as follows:
Array ([0] => jay@zoo .tw
[1] => joe@host.com
[2] => john.doe@gh.co.uk
[3] => asmithsonian@us.info
)
Multidimensional sorting
Finally, PHP also allows you to perform some more complex sorting on multidimensional arrays - for example, first sort a nested array using a common keyword, Then sort based on another keyword. This is very similar to using SQL's ORDER BY statement to sort multiple fields. In order to better understand how it works, please look carefully at the example given by Listing J:
Listing J
Copy code The code is as follows:
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 a row and column array in the $data array . I then use the array_multisort() function to reorder the data set, first by rating, and then, if the ratings are equal, by name. Its output is as follows:
Copy code The 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() function is the most popular in PHP One of the most useful functions, it has a very wide range of applications. In addition, as you can see in the example, it can sort multiple unrelated arrays, it can also use one element as the basis for the next sort, and it 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 of the internal functions hidden in the PHP array processing toolkit.
http://www.bkjia.com/PHPjc/322344.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322344.htmlTechArticleOften, developers find it very useful to sort values or array elements using this data structure in PHP. PHP provides some sorting functions suitable for various arrays. These functions allow...