Array_multisort multi-dimensional array sorting function

Source: Internet
Author: User

The array in the parameter is treated as a table column and sorted by rows-similar to the SQL order by clause function. The first array is the main array to be sorted. If the rows (values) in the array are the same, they are sorted according to the corresponding values in the next input array.

 

<? Php Tutorial
$ A1 = array ("dog", "cat ");
$ A2 = array ("fido", "missy ");
Array_multisort ($ a1, $ a2 );
Print_r ($ a1 );
Print_r ($ a2 );
?> Output:

Array ([0] => cat [1] => dog)
Array ([0] => missy [1] => fido)


Example 6. Ranking

<? Php
$ Grade = array ("score" => array (70, 95, 70.0, 60, "70 "),
"Name" => array ("zhang san", "li si", "wang wu ",
"Zhao liu", "liu qi "));
Array_multisort ($ grade ["score"], sort_numeric, sort_desc,
// Take the score as a value, sorted from high to low
$ Grade ["name"], sort_string, sort_asc );
// Use the name as a string in ascending order
Var_dump ($ grade );
?>

The above example will output:

Array (2 ){
["Score"] =>
Array (5 ){
[0] =>
Int (95)
[1] =>
String (2) "70"
[2] =>
Float (70)
[3] =>
Int (70)
[4] =>
Int (60)
  }
["Name"] =>
Array (5 ){
[0] =>
String (5) "li si"
[1] =>
String (6) "liu qi"
[2] =>
String (7) "wang wu"
[3] =>
String (9) "zhang san"
[4] =>
String (8) "zhao liu"
  }
}


 

Multi-dimensional array sorting:

<? Php
$ Arr = array (1, 2, 3, 4), array ("a", "B", "B", "d "));
Array_multisort ($ arr [0], sort_desc, $ arr [1], sort_asc );
Print_r ($ arr );
?>
Run the sample output:

Array
(
[0] => array
        (
[0] => 4
[1] => 2
[2] => 2
[3] => 1
        )
 
[1] => array
        (
[0] => d
[1] => B
[2] => c
[3] =>
        )
)


The array_multisort () function sorts multiple arrays or multi-dimensional arrays.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.