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);
Âprint_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); Print_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 ("[email protected]", "[email protected]", "[email protected]", "[email protected]"), 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;
}
}
?>
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] = [email protected]
[1] = [email protected]
[2] = [email protected]
[3] = [email protected]
)
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 ("[email protected]", "[email protected]", "[email protected]", "[email protected]"), 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;
}
}
?>
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] = [email protected]
[1] = [email protected]
[2] = [email protected]
[3] = [email protected]
)
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" = "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:
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.
Reprinted from: http://yuninglovekefan.blog.sohu.com/134103390.html
Here are two ways to sort a field in a two-bit array:
How to sort a field in a PHP array 1
Private function arrcmp ($a, $b) {if ($a [' day_time '] = = $b [' day_time ']) {return 0;} return ($a [' Day_time ']< $b [' day_time '])? -1:1;} How to use: Usort ($new, Array ("Developercontroller", "arrcmp")); Array Prototypes:
[3] = = Array ([day_time] = 1291593600 [Two_sum_click_num] = 2)))
This is sorted according to the Day_time field in its array.
How to sort Database Results 2
data
The data in the example is as follows:
Volume | Edition-------+--------67 | 2 86 | 1 85 | 6 98 | 2 86 | 6 67 | 7 |
dataMysql_fetch_assoc ().
<?php $data [] = array (' volume ' => 67, ' Edition ' => 2 ); $data [] = array (' Volume ' => 86, ' Edition ' => 1 ); $data [] = array (' Volume ' => 98, ' Edition ' => 2 ); $data [] = array (' Volume ' => 67, ' Edition ' => 7 |
This example arranges volume in descending order, sorting Edition in ascending order. The
now has an array with rows, but array_multisort () requires an array containing columns, so use the following code to get the columns and sort them.
<?php // 取得列的列表 foreach ($data as $key => $row) { $volume[$key] = $row[‘volume‘]; $edition[$key] = $row[‘edition‘]; }
// 将数据根据 volume 降序排列,根据 edition 升序排列 // 把 $data 作为最后一个参数,以通用键排序 array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data); ?> |
The data set is now sorted and the results are as follows:
Volume | Edition-------+--------98 | 2 86 | 1 86 | 6 85 | 6 67 | 2 67 | 7 |
Sort php arrays and sort by a field