【发布时间】:2019-07-12 22:03:32
【问题描述】:
我通过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 函数吗?
【问题讨论】:
-
if ($order = 1)应该是if ($order === 1)。但这不会引起你的问题。另外,$videoFeed包含什么? -
是的,订单实际上不在我的代码中,它是由用户动态设置的。但我补充说,以避免 cmets 暗示这就是答案。看来我还是做了cmets哈哈。哎呀。更正谢谢。
-
$videoFeed只有一个元素,$xml的值。用 1 个元素对数组进行洗牌没有任何作用。 -
@Eoin 他说的不是
$order == 1行,而是if语句。它使用=分配,而不是使用==进行测试。 -
打电话给
json_decode和json_encode有什么意义?这与$videoFeed = array($xml);基本相同