0

I need to sort an array like this

     [0] => Array
            (
                [id] => 5
                [stats] => Array
                    (
                        [SessionsPlayed] => 1
                    )

            )
     [1] => Array
            (
                [id] => 88
                [stats] => Array
                    (
                        [SessionsPlayed] => 6
                    )

            )
     [2] => Array
            (
                [id] => 22
                [stats] => Array
                    (
                        [SessionsPlayed] => 9
                    )

            )

So that it is in descending order by the SessionPlayed column like this.

     [2] => Array
            (
                [id] => 22
                [stats] => Array
                    (
                        [SessionsPlayed] => 9
                    )

            )
     [1] => Array
            (
                [id] => 88
                [stats] => Array
                    (
                        [SessionsPlayed] => 6
                    )

            )
     [0] => Array
            (
                [id] => 5
                [stats] => Array
                    (
                        [SessionsPlayed] => 1
                    )

            )

I have attempted to look online on how to solve this but all of the answer I find can only sort down one child ( ex. the id column ) Here are the posts I looked at. https://stackoverflow.com/a/16788610/1319033 https://stackoverflow.com/a/2699110/1319033

1

1 Answer 1

3

Use PHP's usort

usort($array, function($a, $b) {
    return $b['stats']['SessionsPlayed'] - $a['stats']['SessionsPlayed'];
    // $b - $a for descending order
});
Sign up to request clarification or add additional context in comments.

1 Comment

The way I showed here. usort sorts an array and uses an user-defines comparing function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.