Grouping sort instances of arrays in PHP

Source: Internet
Author: User

An array of PHP, in which the contents of the array are 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),


    );


to make it easier to express, I'm calling the 3-column number, the three-column ABC

Requirements: The default is sorted by column A, and if column A is the same, the same elements are arranged in reverse order in column C. Column B actually didn't participate in the sort, but it was useful in practical use, so I wrote it.

Method One:

. The code is as follows:


$a = $c = Array ();


foreach ($list as $val) {


$a [] = $val [0]; Column A


$c [] = $val [2]; Column C


}


Install column A ascending, 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);


Method Two:

. The code is as follows:


for ($j =0; $j <count ($list); $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.