按子值排序数组

时间:2017-05-16 10:08:49

标签: php arrays json sorting multidimensional-array

我有一些解码的json数据我想根据子值按特定顺序排序。解码后的json文件结构如下:

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

我使用以下方法输出值没有问题:

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

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

但是现在我想基于喜欢([count])对输出进行排序。我已经尝试了几种方法found in answers here,但我似乎无法以适合我的方式应用他们的解决方案。现在我正在看这个排序:

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

usort($json, 'custom_sort');

这会抛出 usort()期望参数1为数组,给定为空,还有两个count个孩子(评论和喜欢),所以它可能不会起作用因为这一点。

我对使用这些类型的数组很新,所以任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

解决方案:

$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'];
}