随机播放不会改变顺序
Shuffle doesn't change the order
我通过 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
函数?
问题是您在编写 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);
}
我通过 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
函数?
问题是您在编写 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);
}