5

我正在搜索如何基于值构建数组排序器。

我有一个数组输出,如:

key => 0 | id => 16103 | Thumbs => 0 
key => 1 | id => 23019 | Thumbs => 0 
key => 2 | id => 49797 | Thumbs => 5 <- key 2 needs to switch with key 1 
key => 3 | id => 51297 | Thumbs => 0 
key => 4 | id => 58106 | Thumbs => 0 
key => 5 | id => 59927 | Thumbs => 4 <- will be stay at this position 
key => 6 | id => 61182 | Thumbs => 0
key => 7 | id => 68592 | Thumbs => 0 
key => 8 | id => 70238 | Thumbs => 10 <- key 8 needs to switch with key 6 
key => 9 | id => 71815 | Thumbs => 0 
key => 10 | id => 78588 | Thumbs => 0 
etc..

我想编写一个函数来重现上面的数组输出,如下所示。当一个记录有 5 个拇指时,它需要在输出中移动“一个”,当它有 10 个拇指时,它需要高 2 个,依此类推。

我想我应该首先复制数组来为每个输出设置键(prio),比如 100,200,300,这样我们就有足够的空间来设置一行?

提前致谢!

4

2 回答 2

1

我想在你的例子中你最好使用数组数组。(如果你还没有,从问题中不清楚。)像这样。

$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);
于 2013-04-10T09:38:36.507 回答
0

所以,如果我理解正确,你有一个数组数组:

$array = array(
    array('key' => 0, 'id' => 16103, 'Thumbs' => 0), 
    array('key' => 1, 'id' => 23019, 'Thumbs' => 0), 
    array('key' => 2, 'id' => 49797, 'Thumbs' => 5), //<- key 2 needs to switch with key 1 
    array('key' => 3, 'id' => 51297, 'Thumbs' => 0), 
    array('key' => 4, 'id' => 58106, 'Thumbs' => 0), 
    array('key' => 5, 'id' => 59927, 'Thumbs' => 4), //<- will be stay at this position 
    array('key' => 6, 'id' => 61182, 'Thumbs' => 0),
    array('key' => 7, 'id' => 68592, 'Thumbs' => 0), 
    array('key' => 8, 'id' => 70238, 'Thumbs' => 10), //<- key 8 needs to switch with key 6 
    array('key' => 9, 'id' => 71815, 'Thumbs' => 0), 
    array('key' => 10, 'id' => 78588, 'Thumbs' => 0) 
);

在这种情况下,您可以使用usort()

usort($array, function($a, $b){
    $a_thumbs_val = floor($a['Thumbs']/5);
    $a_val = $a_thumbs_val ? $a['key'] - $a_thumbs_val - 0.5 : $a['key'];

    $b_thumbs_val = floor($b['Thumbs']/5);
    $b_val = $b_thumbs_val ? $b['key'] - $b_thumbs_val - 0.5 : $b['key'];
    return ($a_val > $b_val) ? 1 : -1;
});


var_dump($array);

会产生:

array (size=11)
  0 => 
    array (size=3)
      'key' => int 0
      'id' => int 16103
      'Thumbs' => int 0
  1 => 
    array (size=3)
      'key' => int 2
      'id' => int 49797
      'Thumbs' => int 5
  2 => 
    array (size=3)
      'key' => int 1
      'id' => int 23019
      'Thumbs' => int 0
  3 => 
    array (size=3)
      'key' => int 3
      'id' => int 51297
      'Thumbs' => int 0
  4 => 
    array (size=3)
      'key' => int 4
      'id' => int 58106
      'Thumbs' => int 0
  5 => 
    array (size=3)
      'key' => int 5
      'id' => int 59927
      'Thumbs' => int 4
  6 => 
    array (size=3)
      'key' => int 8
      'id' => int 70238
      'Thumbs' => int 10
  7 => 
    array (size=3)
      'key' => int 6
      'id' => int 61182
      'Thumbs' => int 0
  8 => 
    array (size=3)
      'key' => int 7
      'id' => int 68592
      'Thumbs' => int 0
  9 => 
    array (size=3)
      'key' => int 9
      'id' => int 71815
      'Thumbs' => int 0
  10 => 
    array (size=3)
      'key' => int 10
      'id' => int 78588
      'Thumbs' => int 0

注意:应该在同一位置的 2 个条目可能会左右两边,因为 usort 会进行不稳定的排序。您可以通过增强 usort 中的闭包来实现这一点,并添加一些更复杂的比较。

于 2013-04-15T18:44:11.963 回答