代码之家  ›  专栏  ›  技术社区  ›  Eoin

无序播放不会改变顺序

  •  0
  • Eoin  · 技术社区  · 6 年前

    我通过 Simple XML . 我把它从 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);
    

    打印($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 功能?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Eoin    6 年前

    问题是,您在写入时向数据添加了一个额外级别的数组容器。 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);
    }
    
    推荐文章