- rsort: documentation ( source)
- var_dump: documentation ( source)
- array_search: documentation ( source)
- ini_set: documentation ( source)
- error_reporting: documentation ( source)
- array_unique: documentation ( source)
<?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);