We are learning PHP, we may be a multidimensional array of PHP problems to be confused, the article here on the detailed introduction of PHP multi-dimensional array sorting principle. PHP also allows for more complex sorting on multidimensional arrays-for example, a single nested array is sorted by using a common keyword, and then sorted 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 the examples:
- php $data = array(array ("id" => 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); ?>
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 the PHP multi-dimensional array sorting function, and show you some of the internal features hidden in the PHP array processing toolkit. 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
- )
- )
http://www.bkjia.com/PHPjc/446444.html www.bkjia.com true http://www.bkjia.com/PHPjc/446444.html techarticle We are learning PHP, we may be a multidimensional array of PHP problems to be confused, the article here on the detailed introduction of PHP multi-dimensional array sorting principle. PHP is also allowed in ...