0

I have some decoded json data I'd like to sort in a specific order based on a child value. The decoded json file is structured like this:

Array
(
    [location] => Array
        (
            [id] => 10215235726
            [title] => demo title
            [media] => Array
                (
                    [nodes] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 15129696952092
                                    [thumbnail_src] => thumb_15131.jpg
                                    [is_video] => 1
                                    [code] => BTg35sbvdfc
                                    [date] => 1494577207
                                    [display_src] => image_15131.jpg
                                    [video_views] => 318
                                    [caption] => Batman
                                    [comments] => Array
                                        (
                                            [count] => 2
                                        )
                                    [likes] => Array
                                        (
                                            [count] => 87
                                        )
                                )

                            [1] => Array
                                (
                                    [comments_disabled] => 
                                    [id] => 47484867964790738
                                    [thumbnail_src] => thumb_11536.jpg
                                    [is_video] => 
                                    [code] => BTmSAQghufS
                                    [date] => 1493745672
                                    [display_src] => image_11536.jpg
                                    [caption] => Aquaman
                                    [comments] => Array
                                        (
                                            [count] => 2
                                        )
                                    [likes] => Array
                                        (
                                            [count] => 73
                                        )
                                )
etc...

I'm outputting values without problem using the following:

$json = json_decode(file_get_contents("http://www.linktojson.com"), true);

foreach($json['location']['media']['nodes'] as $value) {
    echo $value['display_src'];
}

But now I'd like to sort the output based on likes ([count]). I've tried a couple of methods I found in answers here already, but I can't seem to apply their solutions in a way that works for me. Right now I'm looking at this for sorting:

function custom_sort($a, $b) {
    return $a['count'] - $b['count'];
}

usort($json, 'custom_sort');

This throws usort() expects parameter 1 to be array, null given, also there are two count children (comments and likes) anyway so it probably wouldn't work because of that.

I'm fairly new to working with these types of arrays, so any help would be greatly appreciated.

2
  • So what is unclear? $data_array is not an array. Commented May 16, 2017 at 10:09
  • @u_mulder Sorry about that, copy and paste error on my part. Commented May 16, 2017 at 10:13

1 Answer 1

1

Solutions:

$json = json_decode(file_get_contents("http://www.linktojson.com"), true);

// sorting by comments count
usort($json['location']['media']['nodes'], 'sort_by_comments');

// OR sorting by likes count
usort($json['location']['media']['nodes'], 'sort_by_likes');

// sorting functions:
function sort_by_comments($a, $b) {
    return $a['comments']['count'] - $b['comments']['count'];
}

function sort_by_likes($a, $b) {
    return $a['likes']['count'] - $b['likes']['count'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Aah! Cheers for that mate, so obvious now that I have the solution (as always).
what about the parameter $a and $b?i could not understand about the parameter.please explain it.
@lalithkumar $a and $b are names of parameters passed to implementation of a sort function. Values of $a and $b are elements of array which is currently being sorted.

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.