按键值对 PHP 中的 JSON 对象进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23779412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:56:32  来源:igfitidea点击:

Sort JSON object in PHP by a key value

phpjsonsorting

提问by Zenoo

I need to sort this kind of information by the "score" value in a PHP script, how can I do? :

我需要通过 PHP 脚本中的“分数”值对此类信息进行排序,我该怎么办?:

Array
(
 [0] => stdClass Object
    (
        [name] => Morts par D??shydratation
        [score] => 4
        [id] => dwater
    )

 [1] => stdClass Object
    (
        [name] => R??parations de chantiers
        [score] => 87
        [id] => brep
    )

 [2] => stdClass Object
    (
        [name] => Campeur t??m??raire
        [score] => 77
        [id] => camp
    )

 [3] => stdClass Object
    (
        [name] => D??coration
        [score] => 112
        [id] => deco
    )
)

PS : This is already in a PHP value, I already used json_decode.*

PS:这已经在 PHP 值中了,我已经使用了 json_decode.*

回答by vimist

Where $datalooks like this:

哪里$data看起来像这样:

Array
(
    [0] => stdClass Object
        (
            [name] => Morts par D????shydratation
            [score] => 4
            [id] => dwater
        )

    [1] => stdClass Object
        (
            [name] => R????parations de chantiers
            [score] => 87
            [id] => brep
        )

    [.] => ....
)

You can use usort()to sort the array :

您可以使用usort()对数组进行排序:

<?php                                                                                                                                                                                                       
usort($data, function($a, $b) { //Sort the array using a user defined function
    return $a->score > $b->score ? -1 : 1; //Compare the scores
});                                                                                                                                                                                                        

print_r($data);   
?>

Outputs:

输出:

Array
(
    [0] => stdClass Object
        (
            [name] => D??coration
            [score] => 112
            [id] => deco
        )

    [1] => stdClass Object
        (
            [name] => R??parations de chantiers
            [score] => 87
            [id] => brep
        )

    [2] => stdClass Object
        (
            [name] => Campeur t??m??raire
            [score] => 77
            [id] => camp
        )

    [3] => stdClass Object
        (
            [name] => Drogues
            [score] => 49
            [id] => drug
        )

    [4] => stdClass Object
        (
            [name] => Ouverture de porte
            [score] => 11
            [id] => door
        )

    [5] => stdClass Object
        (
            [name] => Morts par D??shydratation
            [score] => 4
            [id] => dwater
        )

)