3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $original = array(12, 17, 5, 27, 5); // strip duplicates as they are ranked equally $rankings = array_unique($original); // apply some sorting so that the ranks are now given by the keys. rsort($rankings); // now just use the origincal array and lookup the rankings for each value $return = array(); foreach($original as $value) { $rankedValue = array(); $rankedValue['value'] = $value; $rankedValue['rank'] = array_search($value, $rankings) + 1; $return[] = $rankedValue; } var_dump($return);
Output for 4.3.0 - 7.1.0
array(5) { [0]=> array(2) { ["value"]=> int(12) ["rank"]=> int(3) } [1]=> array(2) { ["value"]=> int(17) ["rank"]=> int(2) } [2]=> array(2) { ["value"]=> int(5) ["rank"]=> int(4) } [3]=> array(2) { ["value"]=> int(27) ["rank"]=> int(1) } [4]=> array(2) { ["value"]=> int(5) ["rank"]=> int(4) } }