There is the following PHP array, the contents of the array:
-
- $list = array(
- array(2,3,5),
- array(2,5,24),
- array(3,8,6),
- array(3,2,10) ,
- array(4,7,20),
- array(4,1,15),
- array(6,4,10),
- array(7,9,20),
- );
Copy code
For the convenience of expression, I call the three columns of numbers respectively, the three columns of ABC
Requirements: By default, column A is the main sorting method. If columns A are the same, the same elements are sorted in column C in reverse order. Column B does not actually participate in sorting, but it is useful in practical applications, so it is also written.
method one:
-
- $a = $c = array();
- foreach($list as $val){
- $a[] = $val[0]; //column a
- $c [] = $val[2]; //Column c
- }
- //Install column a in ascending order, and then install column b in descending order, similar to sql, orderby a asc,b desc
- array_multisort($a,SORT_ASC, $c, SORT_DESC , $list);
- print_r($list);
Copy code Method 2:
- for($j=0;$j for($i=count($list)-1;$i>$j ;$i--){
- if($list[$i][0] == $list[$i-1][0] && $list[$i][2] > $list[$i- 1][2])
- list($list[$i],$list[$i-1]) = array($list[$i-1],$list[$i]);
- }
- }
Copy code
|