PHP按某些标准对对象数组进行排序? [英] PHP sorting object array by certain criteria?

查看:48
本文介绍了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;

我如何按播放次数对这些对象进行排序?从我到目前为止所读到的内容中,我明白我应该创建一个比较函数,然后让它与 usort() 一起工作,对吗?但我不太确定如何做到这一点...

how could I sort these objects by playcount? From what I have read so far I understand I should probably create a compare function and then make it work with usort(), right? But I'm not quite sure how to accomplish that...

谢谢




好的,现在我是这样的:




edit: ok, so now I've got it like this:

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... thank you everyone

推荐答案

usort 示例:

在自定义函数中,您只需像这样访问您关心的数组键:

In the custom function you just access the array key you care about like this:

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

打印排序后的数组(只粘贴一个所以不会太长,其他输出相应地按年龄排序)

prints the sorted array (only pasted one so it's not getting to long, other output accordingly sorted by age)

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

这篇关于PHP按某些标准对对象数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆