2010-08-18 34 views
1

我有這樣的事情:PHP按照一定的標準排序對象數組?

$i = 0; 

foreach($tracks['results'] as $track){ 
    $trackName[$i] = $track['name']; 
    $trackPlaycount[$track['name']] = $track['playcount']; 
    $trackPercent[$track['name']] = $track['percent']; 
    $i++; 
} 

$this->trackName = $trackName; 
$this->trackPlaycount = $trackPlaycount; 
$this->trackPercent = $trackPercent; 

我怎麼可能通過playcount這些對象進行排序?從我目前閱讀的內容來看,我明白我應該創建一個比較函數,然後使它與usort()一起工作,對吧?但我不太清楚如何實現這一目標?

謝謝




編輯:好了,現在我有這樣的:

public function firstmethod(){ 
    // there are some more parameters here of course 
    // but they worked before, not important for the problem 
    $i = 0; 
    foreach($tracks['results'] as $track){ 
     $trackName[$i] = $track['name']; 
     $trackPlaycount[$track['name']] = $track['playcount']; 
     $trackPercent[$track['name']] = $track['percent']; 
     // this part is new 
     $tracksArray[$i] = array(
      'name' => $trackName[$i], 
      'playcount' => $trackPlaycount[$track['name']], 
      'percentage' => $trackPercent[$track['name']] 
     ); 
     $i++; 
    } 

    usort($tracksArray, array($this, 'sortByCount')); 

    $i = 0; 
    // this is to put the new sorted array into the old variables? 
    foreach($tracksArray as $temp){ 
     $trackName[$i] = $temp['name']; 
     $trackPlaycount[$trackName[$i]] = $temp['playcount']; 
     $trackPercent[$trackName[$i]] = $temp['percentage']; 
     $i++; 
    } 

    $this->trackName = $trackName; 
    $this->trackPlaycount = $trackPlaycount; 
    $this->trackPercent = $trackPercent; 
} 


public function sortByCount($a, $b){ 
    if($a["playcount"] == $b["playcount"]) { 
     return 0; 
    } 
    return ($a["playcount"] < $b["playcount"]) ? -1 : 1; 
} 

This now works ... 謝謝大家

+0

編輯我的答案,所以它包括整數字段排序,如果這給你的麻煩,希望它可以幫助 – edorian 2010-08-18 12:27:58

回答

7

例如:

在自定義功能,你只需訪問你關心這樣的數組鍵:

?php 
$people = array(
    array('id' => 1, 'name' => 'John', 'age' => 12), 
    array('id' => 2, 'name' => 'Mary', 'age' => 14), 
    array('id' => 3, 'name' => 'Aaaaaadam', 'age' => 15) 
); 

usort($people, "sortByName"); 
var_dump($people); 
usort($people, "sortByAge"); 
var_dump($people); 

function sortByName($a, $b) { 
    return strcmp($a["name"], $b["name"]); 
} 

function sortByAge($a, $b) { // or playcount or whatever 
    if($a["age"] == $b["age"]) { 
     return 0; 
    } 
    return ($a["age"] < $b["age"]) ? -1 : 1; 
} 

打印數組排序(僅粘貼一個,所以它沒有得到長期,其他輸出按年齡相應排序)

array(3) { 
    [0]=> 
    array(3) { 
    ["id"]=> 
    int(3) 
    ["name"]=> 
    string(9) "Aaaaaadam" 
    ["age"]=> 
    int(15) 
    } 
    [1]=> 
    array(3) { 
    ["id"]=> 
    int(1) 
    ["name"]=> 
    string(4) "John" 
    ["age"]=> 
    int(12) 
    } 
    [2]=> 
    array(3) { 
    ["id"]=> 
    int(2) 
    ["name"]=> 
    string(4) "Mary" 
    ["age"]=> 
    int(14) 
    } 
} 
+0

所以,我應該首先填充這個數組數組,我的循環,然後用函數進行排序?還有一件事..因爲這應該全部在一個類內部完成,我可以使這個排序函數爲一個私有方法,然後像usort($ people,$ this-> sortByAge)一樣使用它。或者那種方法不行? – pootzko 2010-08-18 13:11:17

+1

這種方法可以工作,除了像'array($ this,「sortByAge」)',或者像array(「ClassName」,「sortByAge」)這樣的靜態方法傳遞實例方法。 – gnud 2010-08-18 13:48:08

+0

感謝您使用類示例@gnud的回調,+1 – edorian 2010-08-18 14:13:57

5

您也可以使用SplMaxHeap這個。借用@edorian's $people array,你可以做

class SortByAge extends SplMaxHeap 
{ 
    function compare($a, $b) 
    { 
     return $a['age'] - $b['age']; 
    } 
} 

$sort = new SortByAge; 
foreach($people as $person) { 
    $sort->insert($person); 
} 

print_r(iterator_to_array($sort)); 

這應該perform somewhat betterusort

+0

感謝我在查看更多內容時感興趣。 – Chris 2010-08-18 12:35:10