|
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 ease of expression, I called the 3 column numbers, the ABC three-column requirement: By default the column A is the primary, and if the column A is the same, the same element is sorted in reverse column C. Column B actually does not participate in the sort, but it is useful in practical use, so it is also written out. Method One:
- $a = $c = Array ();
- foreach ($list as $val) {
- $a [] = $val [0]; Column A
- $c [] = $val [2]; Column C
- }
- Install column A ascending, and then install column B descending, similar to Sql,orderby a asc,b desc
- Array_multisort ($a, SORT_ASC, $c, Sort_desc, $list);
- Print_r ($list);
Copy Code
Method Two:
- 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 |