我想在你的例子中你最好使用数组数组. (如果你还没有,那么问题就不清楚了.)就像这样.
$array = array();
$array[0] = array('id'=>16103, 'Thumbs'=>0);
$array[1] = array('id'=>16103, 'Thumbs'=>0);
...
然后,从编写交换函数开始.
function swap (&$arr,$key1,$key2) {
$temp=$arr[$key1];
$arr[$key1]=$arr[$key2];
$arr[$key2]=$temp;
// the & before the $arr parameter makes sure the array is passed as a reference. So no need to return the new array at the end.
}
现在为您的排名功能:
function rank(&$arr) {
for ($i = 0; $i < count($arr); $i++) {
if ($arr[$i] < 5) continue;
$places_to_move = $arr[i]['Thumbs'] / 5; // get number of places to promote the entry
$places_to_move = max($places_to_move, $i); // make sure we don't move it out of the array bounds
swap($arr, $i, $i - $places_to_move);
}
}
然后只需为您的未排列数组调用您的rank函数
rank($array);