Home:ALL Converter>Sorting array by child value

Sorting array by child value

Ask Time:2017-05-16T18:08:48         Author:Lenny

Json Formatter

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.

Author:Lenny,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43998484/sorting-array-by-child-value
yy