Maximum value for data sorting

Source: Internet
Author: User
Maximum value for data sorting
Array(    0 => Array    (        'id' => 1,            'level' => 6,            'layer' => 1        ),    '1' => Array    (        'id' => 15055,        [level] => 5,        [layer] => 2    ),    '2' => Array    (        'id' => 15056,        'level' => 5,        'layer' => 3    ),    '3' => Array    (        'id' => 15057,        'level' => 4,        'layer' => 4     ),    '4' => Array    (        'id' => 15058,        'level' => 4,        'layer' => 5    ))

If the level is equal, the array with a larger layer is used.
The final result is as follows:
Array
(
0 => Array
(
'Id' => 1,
'Level' => 6,
'Player' => 1
),



'1' => Array
(
'Id' => 15056,
'Level' => 5,
'Player' => 3
),



'2' => Array
(
'Id' => 15058,
'Level' => 4,
'Player' => 5
)

)


Reply to discussion (solution)

What's the problem? It's not enough to bubble up ....

You can use the usort http://php.net/manual/zh/function.usort.php function.

$a = Array(    0 => Array    (        'id' => 1,            'level' => 6,            'layer' => 1        ),     '1' => Array    (        'id' => 15055,        'level' => 5,        'layer' => 2    ),     '2' => Array    (        'id' => 15056,        'level' => 5,        'layer' => 3    ),     '3' => Array    (        'id' => 15057,        'level' => 4,        'layer' => 4     ),     '4' => Array    (        'id' => 15058,        'level' => 4,        'layer' => 5    ) );$r = array();foreach($a as $v) {  if(! isset($r[$v['level']])) $r[$v['level']] = $v;  if($r[$v['level']]['layer'] < $v['layer']) $r[$v['level']] = $v;}$r = array_values($r);var_export($r);
array (  0 =>   array (    'id' => 1,    'level' => 6,    'layer' => 1,  ),  1 =>   array (    'id' => 15056,    'level' => 5,    'layer' => 3,  ),  2 =>   array (    'id' => 15058,    'level' => 4,    'layer' => 5,  ),)

The landlord's array has changed from five elements to three. can this be achieved by sorting?
Paste the code!

What's the problem? It's not enough to bubble up ....



You can use the usort http://php.net/manual/zh/function.usort.php function.

The landlord's array has changed from five elements to three. can this be achieved by sorting?
Paste the code!


What's the problem? It's not enough to bubble up ....



You can use the usort http://php.net/manual/zh/function.usort.php function.


Haha. I thought it was going to be sorted.

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.