如何使用PHP对嵌套命名空间数组中的值排序数组

时间:2015-10-15 23:39:28

标签: php arrays xml sorting namespaces

我从xml文件生成了一个数组。该文件是有关来自多个YouTube频道的视频的信息。我现在想在PHP中按video:publication_date(降序)对数组进行排序。我的目标是识别并显示xml文件中包含的所有频道的最新10个视频。

如何做到这一点?

我已经看到了许多对简单数组进行排序的解决方案,但我无法弄清楚如何使用PHP对嵌套命名空间数组(如[video:video])中的值进行排序。

print_r($ array)输出如下内容:

Array
(
    [urlset] => Array
        (
            [url] => Array
                (
                    [0] => Array
                        (
                            [loc] => http://www. . . ./vod_page.php?channel=XXxxbnaoqvqbGpCcW9t3V1jQ&vid=xxxQSNyiwMk
                            [video:video] => Array
                                (
                                    [video:thumbnail_loc] => https://i.ytimg.com/vi/xxxQSNyiwMk/hqdefault.jpg
                                    [video:title] => Singing
                                    [video:description] => Show
                                    [video:player_loc] => http://www. . . ./jw57player.swf?file=http://www.youtube.com/watch?v=xxxQSNyiwMk
                                    [video:duration] => 162
                                    [video:view_count] => 38
                                    [video:publication_date] => 2014-12-10T16:01:08.000Z
                                )

                        )

                    [1] => Array
                        (
                            [loc] => http://www. . . ./vod_page.php?channel=XXxxbnaoqvqbGpCcW9t3V1jQ&vid=xxxeDipW7PU
                            [video:video] => Array
                                (
                                    [video:thumbnail_loc] => https://i.ytimg.com/vi/xxxeDipW7PU/hqdefault.jpg
                                    [video:title] => Singing Live
                                    [video:description] => Performing Nov.18th 2014
                                    [video:player_loc] => http://www. . . ./jw57player.swf?file=http://www.youtube.com/watch?v=xxxeDipW7PU
                                    [video:duration] => 244
                                    [video:view_count] => 265
                                    [video:publication_date] => 2014-12-09T11:14:00.000Z
                                )

                        )

                    [2] => Array
                        (
                            [loc] => http://www. . . ./vod_page.php?channel=XXxxbnaoqvqbGpCcW9t3V1jQ&vid=xxxyvRH4NEg
                            [video:video] => Array
                                (
                                    [video:thumbnail_loc] => https://i.ytimg.com/vi/xxxyvRH4NEg/hqdefault.jpg
                                    [video:title] => Making
                                    [video:description] => Performing Nov.15th 2015
                                    [video:player_loc] => http://www. . . ./jw57player.swf?file=http://www.youtube.com/watch?v=xxxyvRH4NEg
                                    [video:duration] => 222
                                    [video:view_count] => 79
                                    [video:publication_date] => 2014-12-09T00:03:06.000Z
                                )

                        )
                    ....

1 个答案:

答案 0 :(得分:1)

考虑以下使用array_multisort(),它需要较低级别的关联数组来对较大的数组进行排序。该解决方案甚至可以为其他排序字段留出空间。

复制数组通知我故意错误地安排了演示的发布日期):

$youtube = array();

$youtube['urlset']['url'][0]['loc'] = "http://www. . . ./vod_page.php?channel=XXxxbnaoqvqbGpCcW9t3V1jQ&vid=xxxyvRH4NEg";
$youtube['urlset']['url'][0]['video:video']['video:thumbnail_loc'] = "https://i.ytimg.com/vi/xxxyvRH4NEg/hqdefault.jpg";
$youtube['urlset']['url'][0]['video:video']['video:title'] = "Making";
$youtube['urlset']['url'][0]['video:video']['video:description'] = "Performing Nov.15th 2015";
$youtube['urlset']['url'][0]['video:video']['video:player_loc'] = "http://www. . . ./jw57player.swf?file=http://www.youtube.com/watch?v=xxxyvRH4NEg";
$youtube['urlset']['url'][0]['video:video']['video:duration'] =  222;
$youtube['urlset']['url'][0]['video:video']['video:view_count'] = 79;
$youtube['urlset']['url'][0]['video:video']['video:publication_date'] = date("2014-12-09T00:03:06.000Z");

$youtube['urlset']['url'][1]['loc'] = "http://www. . . ./vod_page.php?channel=XXxxbnaoqvqbGpCcW9t3V1jQ&vid=xxxeDipW7PU";
$youtube['urlset']['url'][1]['video:video']['video:thumbnail_loc'] = "https://i.ytimg.com/vi/xxxeDipW7PU/hqdefault.jpg";
$youtube['urlset']['url'][1]['video:video']['video:title'] = "Singing Live";
$youtube['urlset']['url'][1]['video:video']['video:description'] = "Performing Nov.18th 2014";
$youtube['urlset']['url'][1]['video:video']['video:player_loc'] = "http://www. . . ./jw57player.swf?file=http://www.youtube.com/watch?v=xxxeDipW7PU";
$youtube['urlset']['url'][1]['video:video']['video:duration'] =  244;
$youtube['urlset']['url'][1]['video:video']['video:view_count'] = 265;
$youtube['urlset']['url'][1]['video:video']['video:publication_date'] = date("2014-12-09T11:14:00.000Z");

$youtube['urlset']['url'][2]['loc'] = "http://www. . . ./vod_page.php?channel=XXxxbnaoqvqbGpCcW9t3V1jQ&vid=xxxQSNyiwMk";
$youtube['urlset']['url'][2]['video:video']['video:thumbnail_loc'] = "https://i.ytimg.com/vi/xxxQSNyiwMk/hqdefault.jpg";
$youtube['urlset']['url'][2]['video:video']['video:title'] = "Singing";
$youtube['urlset']['url'][2]['video:video']['video:description'] = "Show";
$youtube['urlset']['url'][2]['video:video']['video:player_loc'] = "http://www. . . ./jw57player.swf?file=http://www.youtube.com/watch?v=xxxQSNyiwMk";
$youtube['urlset']['url'][2]['video:video']['video:duration'] = 162;
$youtube['urlset']['url'][2]['video:video']['video:view_count'] = 38;
$youtube['urlset']['url'][2]['video:video']['video:publication_date'] = date("2014-12-10T16:01:08.000Z");

实际排序代码

# TO CREATE SORTING ARRAY, $pubdate
$pubdate = array();
foreach ($youtube['urlset']['url'] as $ykey => $yvalue) {        
    $pubdate[] = $yvalue['video:video']['video:publication_date'];
}

# APPLYING SORTING TO ARRAY AT URL LEVEL
array_multisort($pubdate, SORT_DESC, $youtube['urlset']['url']);

输出注意2014-12-10最初位于底部,因为URL [2]现在位于顶部,因为URL [0]是最近的日期

Array
(
    [urlset] => Array
        (
            [url] => Array
                (
                    [0] => Array
                        (
                            [loc] => http://www. . . ./vod_page.php?channel=XXxxbnaoqvqbGpCcW9t3V1jQ&vid=xxxQSNyiwMk
                            [video:video] => Array
                                (
                                    [video:thumbnail_loc] => https://i.ytimg.com/vi/xxxQSNyiwMk/hqdefault.jpg
                                    [video:title] => Singing
                                    [video:description] => Show
                                    [video:player_loc] => http://www. . . ./jw57player.swf?file=http://www.youtube.com/watch?v=xxxQSNyiwMk
                                    [video:duration] => 162
                                    [video:view_count] => 38
                                    [video:publication_date] => 2014-12-10CDT16:01:08.000-18000
                                )

                        )

                    [1] => Array
                        (
                            [loc] => http://www. . . ./vod_page.php?channel=XXxxbnaoqvqbGpCcW9t3V1jQ&vid=xxxeDipW7PU
                            [video:video] => Array
                                (
                                    [video:thumbnail_loc] => https://i.ytimg.com/vi/xxxeDipW7PU/hqdefault.jpg
                                    [video:title] => Singing Live
                                    [video:description] => Performing Nov.18th 2014
                                    [video:player_loc] => http://www. . . ./jw57player.swf?file=http://www.youtube.com/watch?v=xxxeDipW7PU
                                    [video:duration] => 244
                                    [video:view_count] => 265
                                    [video:publication_date] => 2014-12-09CDT11:14:00.000-18000
                                )

                        )

                    [2] => Array
                        (
                            [loc] => http://www. . . ./vod_page.php?channel=XXxxbnaoqvqbGpCcW9t3V1jQ&vid=xxxyvRH4NEg
                            [video:video] => Array
                                (
                                    [video:thumbnail_loc] => https://i.ytimg.com/vi/xxxyvRH4NEg/hqdefault.jpg
                                    [video:title] => Making
                                    [video:description] => Performing Nov.15th 2015
                                    [video:player_loc] => http://www. . . ./jw57player.swf?file=http://www.youtube.com/watch?v=xxxyvRH4NEg
                                    [video:duration] => 222
                                    [video:view_count] => 79
                                    [video:publication_date] => 2014-12-09CDT00:03:06.000-18000
                                )

                        )

                )

        )

)