The PHP array contains the following content:
- $ 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 ),
- );
For ease of expression, I name the three columns of numbers respectively. The ABC column needs to be sorted by column A by default. If column A is the same, the C column sorts the same elements in inverted order. Column B is not involved in sorting, but it is useful in practical use, so it is also written. Method 1:
- $ A = $ c = array ();
- Foreach ($ list as $ val ){
- $ A [] = $ val [0]; // Column
- $ 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 );
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]);
- }
- }
|