10

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

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

0

1 Answer 1

24

Where $data looks like this:

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 :

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

)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but it doesn't work. The data I use is already a PHP value, not a JSON. And when I apply your function to it, it only gives "1" as output.
Can you update your question with the PHP value rather than the JSON one so I can see what the data we're working with looks like?
Yeah here you go, sorry ^^
usort($data, function($a, $b){ return strnatcmp($a->score, $b->score); }); would display it in national order..
If you need an ascedant order use "return $a-> score > $b-> score ? 1 : -1;"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.