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
Copy CodeThe code is as follows:
$data = Array (5,8,1,7,2);
Sort ($data);
Print_r ($data);
?>
The output results are 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 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
Copy CodeThe code is as follows:
?>
It outputs the following results:
Copy CodeThe 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 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
Copy CodeThe code is as follows:
"states", "in" and "India", "DE" = "Germany", "ES" and "Spain"); Ksort ($data); Print_r ($data);
?>
It outputs the following results:
Copy CodeThe code is as follows:
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
Copy CodeThe code is as follows:
"states", "in" and "India", "DE" = "Germany", "ES" and "Spain"); Krsort ($data); Print_r ($data);
?>
It outputs the following results:
Copy CodeThe code is as follows:
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
Copy CodeThe code is as follows:
"states", "in" and "India", "DE" = "Germany", "ES" and "Spain"); Asort ($data); 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
Copy CodeThe code is as follows:
"states", "in" and "India", "DE" = "Germany", "ES" and "Spain"); Arsort ($data); 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.
Copy CodeThe code is as follows:
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
Copy CodeThe code is as follows:
Natsort ($data); Print_r ($data);? >
It outputs the following results:
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 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
Copy CodeThe code is as follows:
?>
Here is the result of its output:
Copy CodeThe code is as follows:
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
Copy CodeThe 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;
}
}
?>
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:
Copy CodeThe 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 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
Copy CodeThe code is as follows:
Natsort ($data); Print_r ($data);? >
It outputs the following results:
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 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
Copy CodeThe code is as follows:
?>
Here is the result of its output:
Copy CodeThe code is as follows:
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
Copy CodeThe 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;
}
}
?>
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:
Copy CodeThe 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 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
Copy CodeThe code is as follows:
1, "name" = "Boney M", "rating" = 3),
Array ("id" = 2, "name" = "Take That", "rating" and "= 1"),
Array ("id" = 3, "name" = "The Killers", "rating" and "= 4"),
Array ("id" = 4, "name" = "Lusain", "rating" and "= 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:
Copy CodeThe code is as follows:
Array ([0] = = Array
(
[id] = 2
[Name]
[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.
http://www.bkjia.com/PHPjc/322344.html www.bkjia.com true http://www.bkjia.com/PHPjc/322344.html techarticle often, developers find it useful to use this data structure in PHP to sort values or array elements. PHP provides some sort functions that are suitable for multiple arrays, which allow ...