2013-04-10 169 views
5

我正在尋找如何基於數值構建數組排序器。基於值的php數組排序器

我有一個像陣列輸出:

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個拇指時,它需要在輸出中移動「1」,當它有10個拇指2更高時,依此類推。

我想我應該重現數組,首先爲每個輸出設置鍵(prio),如100,200,300,所以我們有足夠的空間來設置行之間?

在此先感謝!

+0

所以你想要8鍵在頂部,鍵2在第二個位置,然後每個其他4個或更少的條目?或者鍵2是否按鍵1和鍵8鍵6? – Chris 2013-04-10 09:24:01

+0

我認爲你應該製作一些用來存儲臨時「大拇指」的計數器(一個字段),如果它達到五個,則按此排列數組,然後將其設置爲零,因此它不會增加兩次。 – Voitcus 2013-04-10 09:25:56

+0

@Chris這些結果來自搜索動作,因此它們已具有一定的權重。我不會拋棄所有這些,說大多數大拇指的結果是最好的。我想在搜索算法和user_inputs之間進行一種組合,這就是爲什麼我想按照(通過用戶輸入)給予拇指「輕輕」排名。 5個拇指表示一個位置較高,10個拇指兩個位置較高等。 – directory 2013-04-10 09:38:49

回答

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($array); 
+0

爲什麼downvote?我認爲這是比我的回答更好的方法。 – enrey 2013-04-15 18:45:15

+0

它不會給我排名的結果,通過函數獲取數組提供相同的數組:/你的函數似乎排名的結果好嗎?! – directory 2013-04-16 08:17:54

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沒有定見排序。你可以通過提高封閉性來增加一些更復雜的比較。