在PHP中按键值排序JSON对象[重复]

时间:2022-10-26 16:51:33

This question already has an answer here:

这个问题在这里已有答案:

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。*

1 个解决方案

#1


8  

Where $data looks 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
        )

)

#1


8  

Where $data looks 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
        )

)