php - Sorting a multi-dimensional Array by value -
i have array , want sort (asc)?
$stand_array[$player_name][$player_points] = $player_rank;
print_r
array ( [player1] => array ( [50] => 5.7 ) [player2] => array ( [40] => 4.2 ) [player3] => array ( [30] => 3.7 ) [player4] => array ( [20] => 2.3 ) [player5] => array ( [10 => 1.5 ) [player6] => array ( [60] => 6.3 ) )
would me solve array on $player_rank (asc)?
nb: tried function, did not work:
function sortbyorder($a, $b) { return $a[$player_rank] - $b[$player_rank]; } usort($myarray, 'sortbyorder');
the variable $player_rank
not visible sortbyorder
function scope. also, indexes different in each player array, need access this:
function sortbyorder($a, $b) { $a = end($a); $b = end($b); if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } usort($myarray, 'sortbyorder');
and if want save keys in $myarray
must use uasort
function instead of usort
.
Comments
Post a Comment