随机播放不会改变顺序

时间:2019-07-12 22:03:32

标签: php shuffle

我已经通过Simple XML获得了RSS供稿。 我已将其从arrays / objects更改为所有array

我想将输出随机排列,但是出于某种原因,它总是以日期为基础。

我下面的代码以及我的print_r语句

PHP

$xml = simplexml_load_file($feed);
$order = 1;
$videoFeed   = json_decode(json_encode(array($xml)), true); 
if ($order == 1) {
    shuffle ( $videoFeed );
}
print_r($videoFeed);

Print_r($ videoFeed)

Array
(
    [0] => Array
        (
            [link] => Array
                (
                    [@attributes] => Array
                        (
                            [rel] => self
                            [href] => https://www.youtube.com/feeds/videos.xml?playlist_id=PLx0GbZ0m42LqeIybI2x_bBLGlo85-5VNg
                        )

                )

            [id] => yt:playlist:PLx0GbZ0m42LqeIybI2x_bBLGlo85-5VNg
            [title] => Best Joomla! Videos
            [author] => Array
                (
                    [name] => Eoin Oliver
                    [uri] => https://www.youtube.com/channel/UCGkX76DCQlWTdjP3CWNbC-A
                )

            [published] => 2019-06-28T17:37:33+00:00
            [entry] => Array
                (
                    [0] => Array
                        (
                            [id] => yt:video:fouYgPJR5Jc
                            [title] => JD19AT  - Optimizing the Joomla back-end
                            [link] => Array
                                (
                                    [@attributes] => Array
                                        (
                                            [rel] => alternate
                                            [href] => https://www.youtube.com/watch?v=fouYgPJR5Jc
                                        )

                                )

                            [author] => Array
                                (
                                    [name] => J and Beyond e.V.
                                    [uri] => https://www.youtube.com/channel/UCy6ThiEDnalZOd_pgtpBk1Q
                                )

                            [published] => 2019-03-30T16:25:40+00:00
                            [updated] => 2019-04-09T20:16:34+00:00
                        )

                    [1] => Array
                        (
                            [id] => yt:video:70Kx00H_cLI
                            [title] => JD19AT  - KEYNOTE - Introducing Joomla 4 for Content Creators
                            [link] => Array
                                (
                                    [@attributes] => Array
                                        (
                                            [rel] => alternate
                                            [href] => https://www.youtube.com/watch?v=70Kx00H_cLI
                                        )

                                )

                            [author] => Array
                                (
                                    [name] => J and Beyond e.V.
                                    [uri] => https://www.youtube.com/channel/UCy6ThiEDnalZOd_pgtpBk1Q
                                )

                            [published] => 2019-03-30T08:55:39+00:00
                            [updated] => 2019-04-29T13:10:49+00:00
                        )
~

提要有效,但总是顺序相同。我误解了$shuffle函数吗?

1 个答案:

答案 0 :(得分:1)

问题是您在写入array($xml)时向数据添加了额外级别的数组容器。因此,$videoFeed中只有一个元素,重新排序没有任何作用。

尝试

$videoFeed   = json_decode(json_encode($xml), true); 

然后,问题在于提要项不是此数据的顶层,它们在数组的entry元素中。所以你应该这样做:

if ($order == 1) {
    shuffle($videoFeed['entry']);
}

为避免过度使用['entry']和避免array_chunk出现问题,可以将数组上移一个步骤。首先使其成为阵列,以防将来需要阵列的任何部分。然后像这样向上移动:

$videoFeed      = json_decode(json_encode($xml), true); 
$videoFeedEntry = $videoFeed['entry'];
if ($order == 1) {
    shuffle($videoFeedEntry);
}
相关问题