If you've been using PHP for a while, you should already be familiar with its array-a data structure that allows you to store multiple values in a single variable and manipulate them as a collection.
Often, developers find it useful to use this data structure in PHP to sort values or array elements. PHP provides a number of sorting functions that are suitable for multiple arrays, which allow you to arrange elements within an 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 sort.
Simple sorting
First, let's take a look at the simplest case: simply sort an array element from low to high, which can be arranged numerically or alphabetically. The PHP sort () function implements this function, as shown in Listing A :
Listing A
<?php$data = Array (5,8,1,7,2); sort ($data);p rint_r ($data);? >
The output results are as follows:
Array ([0] = 1[1] [2[2] = 5[3] = 7[4] = 8)
You can also use the Rsort () function to sort the result, which is the opposite of the sort () simple sort result used earlier. The Rsort () function is inverted from high to low on the array elements, and can also be arranged numerically, in alphabetical order. Listing B shows us an example of it:
Listing B
<?php $data = Array (5,8,1,7,2), Rsort ($data);p rint_r ($data);? >
It outputs the following results:
Array ([0] = 8[1] [7[2] = 5[3] = 2[4] = 1)
Sort by keyword
When we use arrays, we often reorder them from high to low according to the keyword. The Ksort () function is a function that sorts according to the keyword, while it maintains the relevance of the keyword during sorting. Listing C is an example:
Listing C
<?php $data = Array ("US" = "states", "in" = "India", "DE" = "Germany", "ES" and "Spain"); Ksort ($da TA); Print_r ($data);? >
It outputs the following results:
Array ([DE] = germany[es] = Spain[in] = India[us] = States)
The Krsort () function is inverted according to the keyword array, Listing d is an example of this:
Listing D
<?php $data = Array ("US" = "states", "in" = "India", "DE" = "Germany", "ES" and "Spain"); Krsort ($d ATA); Print_r ($data);? >
It outputs the following results:
Array ([US] = states[in] = India[es] = Spain[de] = Germany)
Sort by value
If you want to use value ordering instead of keyword ordering, PHP will also meet your requirements. You can just use the Asort () function instead of the previously mentioned Ksort () function. As shown in Listing E :
Listing E
<?php $data = Array ("US" = "states", "in" = "India", "DE" = "Germany", "ES" and "Spain"); Asort ($da TA); Print_r ($data);? >
Here is the output of the result. Note that this result differs from the result of using the Ksort () function above-in both cases, in alphabetical order, but they are sorted according to the different fields of the array.
Also, note that the relationship between the keyword-value is always maintained; it's just a way to sort the keyword-value pairs, and the sort doesn't change their correspondence.
Array ([DE] = germany[in] = India[es] = Spain[us] = States)
Now, you can certainly guess this sort can also be inverted, which uses the Arsort () function to accomplish this function. Listing F is an example:
Listing F
<?php $data = Array ("US" = "states", "in" = "India", "DE" = "Germany", "ES" and "Spain"); Arsort ($d ATA); Print_r ($data);? >
The following is the result of its output, which is inverted alphabetically by value. It is easy to understand the difference between the following results compared to the results generated after the Krsort () function is inverted.
Array ([US] = states[es] = Spain[in] = India[de] = Germany)
Natural language Sorting
PHP has a very unique sort of way of using cognition rather than using computational rules. This feature is called Natural language ordering, which is useful when creating fuzzy logic applications. Below you can look at a simple example of it, as shown in Listing G :
Listing G
<?php $data = Array ("Book-1", "book-10", "book-100", "book-5"); Sort ($data);p rint_r ($data); Natsort ($data); Print_r ($data);? >
It outputs the following results:
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 ranking results more intuitive, more "humane", but the first is more in line with the algorithm rules, more "computer" features.
Can natural language be inverted? The answer is YES! As long as you use the Array_reverse () function for the results of Natsort (),Listing H is a simple example:
Listing H
<?php $data = Array ("Book-1", "book-10", "book-100", "book-5"); Natsort ($data); Print_r (Array_reverse ($data));? >
Here is the result of its output:
Array ([0] = book-100[1] = book-10[2] = book-5[3] = book-1)
Sort based on user-defined rules
PHP also allows you to define your own sorting algorithm, and you can create your own comparison function and pass it to the Usort () function. If the first parameter is "smaller" than the second argument, the comparison function must return a number smaller than 0, and if the first argument is "larger" than the second argument, the comparison function should return a number greater than 0.
Listing I is an example of an array of elements sorted by their length in this example, with the shortest entry in the front:
Listing I
<?php $data = Array ("joe@host.com", "john.doe@gh.co.uk", "Asmithsonian@us.info", "jay@zoo.tw"), Usort ($data, ' Sortbylen ');p rint_r ($data); function Sortbylen ($a, $b) {if (strlen ($a) = = strlen ($b)) {return 0;} else {return (strlen ($a) > strlen ($b))? 1:-1;} }?>
This creates our own comparison function, which 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. Here is the result of its output:
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 sort of way of using cognition rather than using computational rules. This feature is called Natural language ordering, which is useful when creating fuzzy logic applications. Below you can look at a simple example of it, as shown in Listing G :
Listing G
<?php $data = Array ("Book-1", "book-10", "book-100", "book-5"); Sort ($data);p rint_r ($data); Natsort ($data); Print_r ($data);? >
It outputs the following results:
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 ranking results more intuitive, more "humane", but the first is more in line with the algorithm rules, more "computer" features.
Can natural language be inverted? The answer is YES! As long as you use the Array_reverse () function for the results of Natsort (),Listing H is a simple example:
Listing H
<?php $data = Array ("Book-1", "book-10", "book-100", "book-5"); Natsort ($data); Print_r (Array_reverse ($data));? >
Here is the result of its output:
Array ([0] = book-100[1] = book-10[2] = book-5[3] = book-1)
Sort based on user-defined rules
PHP also allows you to define your own sorting algorithm, and you can create your own comparison function and pass it to the Usort () function. If the first parameter is "smaller" than the second argument, the comparison function must return a number smaller than 0, and if the first argument is "larger" than the second argument, the comparison function should return a number greater than 0.
Listing I is an example of an array of elements sorted by their length in this example, with the shortest entry in the front:
Listing I
<?php $data = Array ("joe@host.com", "john.doe@gh.co.uk", "Asmithsonian@us.info", "jay@zoo.tw"), Usort ($data, ' Sortbylen ');p rint_r ($data); function Sortbylen ($a, $b) {if (strlen ($a) = = strlen ($b)) {return 0;} else {return (strlen ($a) > strlen ($b))? 1:-1;} }?>
This creates our own comparison function, which 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. Here is the result of its output:
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 for more complex sorting on multidimensional arrays-for example, first sorting a nested array with a common keyword and then sorting by another keyword. This is similar to sorting multiple fields with an order BY statement that uses SQL. To get a better idea of how it works, take a closer look at Listing J 's example:
Listing J
<?php $data = array ("id" = = 1, "name" = "Boney M", "rating" = 3), array ("id" = 2, "name" = "Tak E that "," rating "= 1), array (" id "= 3," name "=" The Killers "," rating "= 4), array (" id "= 4," name "=& Gt "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 rearrange the data collection, starting with rating, and then sorting by name if the rating is equal. It outputs the following results:
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))
The Array_multisort () function is one of the most useful functions in PHP, and it has a very wide range of applications. Also, as you can see in the example, it is possible to sort multiple unrelated arrays, use one of them as the basis for the next sort, and sort the database result set.
These examples should give you a basic understanding of the use of various array sorting functions in PHP, and show you some of the internal features hidden in the PHP array processing toolkit.