Home  >  Article  >  Backend Development  >  Detailed explanation of array_multisort function of PHP array

Detailed explanation of array_multisort function of PHP array

小云云
小云云Original
2018-03-27 15:01:123191browse

The array_multisort function in PHP sorts multiple arrays or multidimensional arrays. The parameter array is treated as a column of a table and sorted by rows - this is similar to the function of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are compared to be the same, they are sorted according to the size of the corresponding value in the next input array, and so on. Note: The number of array elements used as parameters should be consistent, otherwise an error will be reported.

Let’s first look at an example of sorting multiple arrays.

  1. $array1 = array('one'=>'10','two'=>'20','three'=>'20','four'=>10);  
    $array2 = array('one'=>'10','two'=>'30','three'=>'20','four'=>'1');  
    $array3 = array('one'=>'C','two'=>'A','three'=>'B','four'=>'F');  
      
    array_multisort($array1,$array2,$array3);  
    print_r($array1);//Array ( [four] => 10 [one] => 10 [three] => 20 [two] => 20 )  
    print_r($array2);//Array ( [four] => 1 [one] => 10 [three] => 20 [two] => 30 )  
    print_r($array3);//Array ( [four] => F [one] => C [three] => B [two] => A )


In the above example, the first parameter array is first sorted (all arrays are sorted in ascending order by default), we can see that the The same values ​​exist in an array (the key names 'one' and 'four' have the same key value, and the key names 'two' and 'three' have the same key value), so when sorting the same value in the first array, Sort by the size of the corresponding value in the next input array (the value of the second array 'four' is smaller than the value of 'one', so the value of four is sorted in front of one), and so on.

Looking at an example of changing the sort order.

  1. $array1 = array('one'=>'10','two'=>'20','three'=>'20','four'=>10);  
    $array2 = array('one'=>'10','two'=>'30','three'=>'20','four'=>'1');  
    $array3 = array('one'=>'C','two'=>'A','three'=>'B','four'=>'F');  
      
    array_multisort($array1,SORT_DESC,$array2,SORT_ASC,$array3);  
    print_r($array1);//Array ( [three] => 20 [two] => 20 [four] => 10 [one] => 10 )  
    print_r($array2);//Array ( [three] => 20 [two] => 30 [four] => 1 [one] => 10 )  
    print_r($array3);//Array ( [three] => B [two] => A [four] => F [one] => C )

In this example, the first array is sorted in descending order, and when the same value is encountered, the second array is sorted according to the ascending value.

Note: If the sorted arrays are all associative arrays, the original key names will be retained. If there is an index array, the index will be re-established in order.

  1. $array1 = array('one'=>'10',2=>'20',3=>'20',4=>10);    
    $array2 = array('one'=>'10','2'=>'30','3'=>'20','four'=>'1');    
    $array3 = array('one'=>'C','2'=>'A','3'=>'B','four'=>'F');    
        
    array_multisort($array1,$array2,$array3);    
      
    print_r($array1); //Array ( [0] => 10 [one] => 10 [1] => 20 [2] => 20 )   
    print_r($array2); //Array ( [four] => 1 [one] => 10 [0] => 20 [1] => 30 )   
    print_r($array3); //Array ( [four] => F [one] => C [0] => B [1] => A )

Multidimensional array sorting.

We usually have some multi-dimensional arrays that need to be sorted:

  1. $guys = array(  
        array('name'=>'jake', 'score'=>80, 'grade' =>'A'),  
        array('name'=>'jina', 'score'=>70, 'grade'=>'A'),  
        array('name'=>'john', 'score'=>70, 'grade' =>'A'),  
        array('name'=>'ben', 'score'=>20, 'grade'=>'B')  
    );  
    //例如我们想按成绩倒序排列,如果成绩相同就按名字的升序排列。  
    //这时我们就需要根据$guys的顺序多弄两个数组出来:  
    $scores = array(80,70,70,20);  
    $names = array('jake','jina','john','ben');  
    //然后  
    array_multisort($scores, SORT_DESC, $names, $guys);  
      
    foreach($guys as $v){  
        print_r($v);  
        echo "<br/>";  
    }  
    /* 
    Array ( [name] => jake [score] => 80 [grade] => A ) 
    Array ( [name] => jina [score] => 70 [grade] => A ) 
    Array ( [name] => john [score] => 70 [grade] => A ) 
    Array ( [name] => ben [score] => 20 [grade] => B ) 
    */

Related recommendations:

php for multiple Array or multi-dimensional array sorting function array_multisort()

10 recommended articles about array_multisort()

PHP array_multisort() function Notes on use

The above is the detailed content of Detailed explanation of array_multisort function of PHP array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn