Home > Backend Development > PHP Tutorial > Example of grouping sorting of arrays in PHP_PHP tutorial

Example of grouping sorting of arrays in PHP_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:29:09
Original
781 people have browsed it

PHP array, the content in the array is roughly as follows:

Copy code 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 the convenience of expression, I call the three columns of numbers respectively, the three columns of ABC

Requirement: By default, column A is the main sorting method. If column A is the same, the same elements will be sorted in column C in reverse order. Column B does not actually participate in sorting, but it is useful in practical applications, so I also wrote it out.

Method 1:

Copy the code 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 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:
Copy code 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]);
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/779000.htmlTechArticlePHP array, the contents of the array are roughly as follows: Copy the code 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) , a...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template