【问题标题】:Shuffle doesn't change the order随机播放不会改变顺序
【发布时间】: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_decodejson_encode有什么意义?这与$videoFeed = array($xml); 基本相同

标签: php shuffle


【解决方案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);
}

【讨论】:

  • 这样做我得到更多错误:注意:未定义的偏移量:第 76 行 /default.php 中的 0 警告:array_chunk() 期望参数 1 为数组,在 /default.php 中给出 null第 76 行警告:在第 78 行的 default.php 中为 foreach() 提供的参数无效 76: $arrayChunks = array_chunk($videoFeed[0]['entry'], 4); 78: foreach($arrayChunks as $items) {
  • 如果我将其改回json_decode(json_encode(array($xml)), true),我会收到警告:shuffle() 期望参数 1 为数组,在第 69 行的 /default.php 中给出 null
  • 如果你这样做,你需要这样做shuffle($videoFeed[0]['entry'])
  • 您正在添加一个额外的数组分组,因此您必须使用额外级别的索引。
  • shuffle() 期望参数 1 为数组 & array_chunk() 期望参数 1 为数组 & 为 foreach() 提供的参数无效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 2014-04-08
  • 1970-01-01
  • 2020-12-10
  • 2023-01-23
  • 1970-01-01
相关资源
最近更新 更多