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.