2011-02-09 23 views
2

我有關聯數組的數組:如何基於包含的關聯數組在PHP中對數組進行排序?

a[0] = array("value" => "fred", "score" => 10); 
a[1] = array("value" => "john", "score" => 50); 
a[2] = array("value" => "paul", "score" => 5); 

我想基於相關陣列的「分數」值進行排序我的「一個」陣列

它會成爲:

a[0] = array("value" => "paul", "score" => 5); 
a[1] = array("value" => "fred", "score" => 10); 
a[2] = array("value" => "john", "score" => 50); 

有人可以幫我嗎?

回答

4

您需要使用和比較功能。

喜歡的東西:

function cmp($a, $b) 
{ 
    if ($a['score'] == $b['score']) { 
     return 0; 
    } 
    return ($a['score'] < $b['score']) ? -1 : 1; 
} 
+0

優秀的,謝謝,我沒有看到這個功能 – Fredv 2011-02-09 22:51:31

相關問題