PHP按一定标准排序对象数组?

时间:2010-08-18 12:18:55

标签: php sorting object

我有这样的事情:

$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;
}   

这现在有效...... 谢谢大家

2 个答案:

答案 0 :(得分:7)

usort示例:

在自定义函数中,您只需访问您关注的数组键:

?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)
  }
}

答案 1 :(得分: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 better而不是usort