Data sorted by Maximum value

Source: Internet
Author: User
Array (    0 = = Array    (        ' id ' = = 1,            ' level ' = 6,            ' layer ' = 1        ),    ' 1 ' = = array    (        ' id ' = = 15055,        [level] = 5,        [layer] = 2    ),    ' 2 ' = = Array    (        ' id ' =& Gt 15056,        ' level ' = 5,        ' Layer ' and ' 3 '    ,    ' 3 ' = = Array    (        ' id ' = = 15057,        ' Level ' = 4,        ' layer ' + 4     ),    ' 4 ' = = Array    (        ' id ' = = 15058,        ' level ' = 4,< c28/> ' layer ' = 5    ))

If level is equal, the array with the larger layer is taken
The final result is this:
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
)

)


Reply to discussion (solution)

What's the problem? Bubble up a little bit ....

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

$a = Array (        ' id ' = = 1,            ' level ' = 6,            ' layer ' = 1        ),     ' 1 ' = array< c7/> (        ' id ' = = 15055,        ' level ' = + 5,        ' layer ' = 2    ),     ' 2 '    = = Array (        ' id ' = > 15056,        ' level ' = 5,        ' layer ' + 3    ),     ' 3 ' = = Array    (        ' id ' = = 15057,        ' Level ' = 4,        ' layer ' and ' 4 '     ,     ' 4 ' = = Array    (        ' id ' = = 15058,        ' level ' = 4,< c28/> ' 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 from 5 elements into 3, which can be achieved by the sort?
Post code, look!

What's the problem? Bubble up a little bit ....



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

The landlord's array from 5 elements into 3, which can be achieved by the sort?
Post code, look!


What's the problem? Bubble up a little bit ....



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


Haha, look wrong, I thought he was going to sort.
  • 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.