3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $data = array( array('id' => 1, 'name' => 'John Snow', 'score' => 80), array('id' => 2, 'name' => 'John Fire', 'score' => 50), array('id' => 3, 'name' => 'John Water', 'score' => 79), array('id' => 4, 'name' => 'John Leaf', 'score' => 80), ); function cmp($a, $b) { if ($a['score'] == $b['score']) { return 0; } return ($a['score'] > $b['score']) ? -1 : 1; } uasort($data, 'cmp'); $rank = 1; $prev = false; foreach ($data as $i=>$d) { if (!$prev) { $data[$i]['rank'] = $rank; $prev = $d['score']; continue; } elseif ($prev != $d['score']) { $rank++; } $data[$i]['score'] = $rank; $prev = $d['score']; } print_r($data);
Output for hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Array ( [0] => Array ( [id] => 1 [name] => John Snow [score] => 80 [rank] => 1 ) [3] => Array ( [id] => 4 [name] => John Leaf [score] => 1 ) [2] => Array ( [id] => 3 [name] => John Water [score] => 2 ) [1] => Array ( [id] => 2 [name] => John Fire [score] => 3 ) )
Output for 4.3.0 - 5.6.28
Array ( [3] => Array ( [id] => 4 [name] => John Leaf [score] => 80 [rank] => 1 ) [0] => Array ( [id] => 1 [name] => John Snow [score] => 1 ) [2] => Array ( [id] => 3 [name] => John Water [score] => 2 ) [1] => Array ( [id] => 2 [name] => John Fire [score] => 3 ) )