3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(E_ALL | E_STRICT); ini_set('display_errors', 1); // your original input $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);

preferences:
29.47 ms | 402 KiB | 5 Q