PHP array grouping and sorting instance _ php instance

Source: Internet
Author: User
This article mainly introduces the PHP array grouping and sorting example. For more information about the PHP array grouping and sorting method, see The PHP array. the array content is roughly as follows:

The code is as follows:


$ 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 three columns of numbers and three columns of ABC respectively.

Requirement: columns A are sorted by default. If column A is the same, columns C are arranged in reverse order with the same elements. Column B is actually not involved in sorting, but it is useful in practical use, so I also wrote it out.

Method 1:

The code is as follows:


$ 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:

The code is as follows:


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]);
}
}

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.