如何按特定键的值对JSON对象进行排序

时间:2012-12-23 23:20:20

标签: php json sorting

我的目标:

  

按积分排序用户的json列表。

我的问题:

  

信用证按顺序排列,但顺序相反。

我的结果:

Position: 1
Username: abc
Credits: 12

Position: 2
Username: bcd
Credits: 123

Position: 3
Username: cde
Credits: 1245

我需要什么:

Position: 1
Username: abc
Credits: 1234

Position: 2
Username: bcd
Credits: 123

Position: 3
Username: cde
Credits: 12

我的问题:

  

如何按名称排序名称?

我的PHP代码:

<?php
$json = '{"id":1,"jsonrpc":"2.0","total":50,"result":[{"id":1,"username":"Ashley","credits":684921},{"id":2,"username":"Caroline","credits":113045},{"id":3,"username":"Alexandra","credits":669133},{"id":4,"username":"Katelyn","credits":735955},{"id":5,"username":"Ashley","credits":202478},{"id":6,"username":"Anna","credits":854266},{"id":7,"username":"Lily","credits":406425},{"id":8,"username":"Samantha","credits":430603},{"id":9,"username":"Caroline","credits":380785},{"id":10,"username":"Paige","credits":961596},{"id":11,"username":"Bailey","credits":67660},{"id":12,"username":"Kimberly","credits":610751},{"id":13,"username":"Maya","credits":446787},{"id":14,"username":"Maria","credits":818538},{"id":15,"username":"Ella","credits":789682},{"id":16,"username":"Olivia","credits":906604},{"id":17,"username":"Arianna","credits":177500},{"id":18,"username":"Maya","credits":592490},{"id":19,"username":"Aubrey","credits":58834},{"id":20,"username":"Sophie","credits":324357},{"id":21,"username":"Julia","credits":288688},{"id":22,"username":"Rachel","credits":932145},{"id":23,"username":"Arianna","credits":848696},{"id":24,"username":"Valeria","credits":272258},{"id":25,"username":"Autumn","credits":301136},{"id":26,"username":"Audrey","credits":317305},{"id":27,"username":"Chloe","credits":975642},{"id":28,"username":"Payton","credits":510533},{"id":29,"username":"Morgan","credits":927493},{"id":30,"username":"Mya","credits":166566},{"id":31,"username":"Lauren","credits":840207},{"id":32,"username":"Trinity","credits":255691},{"id":33,"username":"Jocelyn","credits":272653},{"id":34,"username":"Madeline","credits":58344},{"id":35,"username":"Valeria","credits":972228},{"id":36,"username":"Chloe","credits":919646},{"id":37,"username":"Jessica","credits":390647},{"id":38,"username":"Claire","credits":670709},{"id":39,"username":"Savannah","credits":47721},{"id":40,"username":"Zoe","credits":214133},{"id":41,"username":"Taylor","credits":991001},{"id":42,"username":"Gabrielle","credits":295506},{"id":43,"username":"Katherine","credits":295472},{"id":44,"username":"Vanessa","credits":600895},{"id":45,"username":"Olivia","credits":337541},{"id":46,"username":"Mackenzie","credits":32221},{"id":47,"username":"Bella","credits":461298},{"id":48,"username":"Maria","credits":318094},{"id":49,"username":"Lillian","credits":632622},{"id":50,"username":"Kayla","credits":17062}]}';
$decoded = json_decode($json);
$users = $decoded->result;

function my_sort($a, $b)
{
    if ($a-credits > $b->credits) {
        return -1;
    } else if ($a->credits < $b->credits) {
        return 1;
    } else {
        return 0; 
    }
}
rsort($users, 'my_sort');
   $position = 0;
foreach($users as $user){
   $position = $position + 1;
   $username = $user->username;
   $credits = $user->credits;
   //do something with it:
   echo "Postion: $position<br/>\n Username: $username<br/>\n  Credits:$credits <br/>\n <br/>\n";
   if ($position == 3) {
        break;
    }
}
?>

2 个答案:

答案 0 :(得分:4)

function my_sort($a, $b)
{
    if ($a->credits > $b->credits) {
        return -1;
    } else if ($a->credits < $b->credits) {
        return 1;
    } else {
        return 0; 
    }
}

usort($users, 'my_sort');

答案 1 :(得分:1)

您需要使用usort代替rsort 请注意,使用php7,您可以执行太空飞船运营商:

function mySort($a, $b) {
    return $b->credits <=> $a->credits;
}

usort($users, 'mySort');