如果重复,则创建数组并合并

时间:2018-03-08 09:28:19

标签: php arrays array-merge

我不确定你会把这个任务称为什么,但希望标题可能会出现在其中一些!

让我们开始..

我有一个包含.m3us的文件夹。 m3us实际上是与音乐有关的流派。所以你会有00s.m3u,50s.m3u等。在这些文件中只有普通的URL,后跟换行到MP3的确切路径。这用于搜索机制,但我们现在已经转移到SQLITE。

内部m3us示例:

http://streamlink.com/rihanna%20-%20work.mp3
http://streamlink.com/taylor%20swift-%20love%20story.mp3

我的目标是遍历每个m3u(流派),使用与该轨道相关的信息创建这些轨道的数组。所以你会有artistname,trackname,genre1,genre2,genre3。如果它来自00s类型。它将为genre1-3创建一个包含00的数组。看到没有其他记录!但是,如果我们说http://streamlink.com/rihanna%20-%20work.mp3出现在00s& 50多岁。然后我想让它制作一个包含所有曲目的最终数组!完全准备好让我创建我的sqlite文件并继续我的业务。回到00s& 50s问题,如果我有多种类型的完全相同的rihanna轨道。我希望它然后在最终的项目数组中创建一个最终项目,填充我的类型1 - 3.所以我将留下:蕾哈娜,工作,00年代,50年代," genre3可以在这里空白或你可以如果没有其他类型填写"则填写00或50。现在不能超过3种类型!所以,如果我们在00年代,50年代,60年代都有舞蹈,那就跳舞吧。它只会放在它找到的前3个!所以它将留下:蕾哈娜,工作,00年代,50年代,60年代,舞蹈不会去任何地方!所以无论如何,我想要一个由我的所有曲目组成的最终数组,其中完全标记了类型为1 - 3的信息当然充满了它所遇到的任何类型。或者只是第一个类型填充到1 - 3所以如果它只是发现rihanna一次,只在00s。然后它应该显示00s,00s,00s。或者只是00s,"",""它并不重要!

我坚持的部分是我不知道如何创建最终阵列,包括00s,50s,60s,如果rihanna出现在那些。我目前循环遍历每个文件,创建数据并推送到数组。我不知道从哪里去!

我的代码:

<?PHP
$Genres = scandir("/var/lib/mpd/playlists");

$testArray = [];

foreach($Genres as $genre)
{
    if($genre !== "." && $genre !== "..")
    {
        $StrippedGenreName = str_replace("Genres - ", "", str_replace(".m3u","",$genre));

        $GenreArray = explode("\n", file_get_contents("/var/lib/mpd/playlists/".$genre));

        foreach($GenreArray as $genredata)
        {
            $TrackPath = $genredata;

            $RawForm = str_replace(".mp3", "", str_replace("%20", " ", str_replace("http://streamlink.com/", "", $genredata)));
            $TrackDecide = explode(" - ", $RawForm);
            $ArtistName = $TrackDecide[0];
            $TrackName = $TrackDecide[1];
            $GenreData = array("",$TrackPath,$ArtistName,$TrackName,"","",$StrippedGenreName,$StrippedGenreName,$StrippedGenreName,"","Audio");
            array_push($testArray, $GenreData);
        }
    }
}
print("<pre>".print_r($testArray,true)."</pre>");

//Code here to do final array task
?>

我的输出:

Array
(
    [0] => Array
        (
            [0] => 
            [1] => http://streamlink.com/2Pac%20Feat.%20Elton%20John%20-%20Ghetto%20Gospel.mp3
            [2] => 2Pac Feat. Elton John
            [3] => Ghetto Gospel
            [4] => 
            [5] => 
            [6] => 00s
            [7] => 00s
            [8] => 00s
            [9] => 
            [10] => Audio
        )

    [1] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Aaliyah%20-%20Try%20Again.mp3
            [2] => Aaliyah
            [3] => Try Again
            [4] => 
            [5] => 
            [6] => 00s
            [7] => 00s
            [8] => 00s
            [9] => 
            [10] => Audio
        )

    [2] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Adele%20-%20Chasing%20Pavements.mp3
            [2] => Adele
            [3] => Chasing Pavements
            [4] => 
            [5] => 
            [6] => 00s
            [7] => 00s
            [8] => 00s
            [9] => 
            [10] => Audio
        )

    [3] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Al%20Hibbler%20-%20Unchained%20Melody.mp3
            [2] => Al Hibbler
            [3] => Unchained Melody
            [4] => 
            [5] => 
            [6] => 50s
            [7] => 50s
            [8] => 50s
            [9] => 
            [10] => Audio
        )

    [4] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Al%20Martino%20-%20Here%20In%20My%20Heart.mp3
            [2] => Al Martino
            [3] => Here In My Heart
            [4] => 
            [5] => 
            [6] => 50s
            [7] => 50s
            [8] => 50s
            [9] => 
            [10] => Audio
        )

    [5] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Alma%20Cogan%20-%20Dreamboat.mp3
            [2] => Alma Cogan
            [3] => Dreamboat
            [4] => 
            [5] => 
            [6] => 50s
            [7] => 50s
            [8] => 50s
            [9] => 
            [10] => Audio
        )

    [6] => Array
        (
            [0] => 
            [1] => http://streamlink.com/2Pac%20Feat.%20Elton%20John%20-%20Ghetto%20Gospel.mp3
            [2] => 2Pac Feat. Elton John
            [3] => Ghetto Gospel
            [4] => 
            [5] => 
            [6] => 50s
            [7] => 50s
            [8] => 50s
            [9] => 
            [10] => Audio
        )

    [7] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Acker%20Bilk%20-%20Stranger%20On%20The%20Shore.mp3
            [2] => Acker Bilk
            [3] => Stranger On The Shore
            [4] => 
            [5] => 
            [6] => 60s
            [7] => 60s
            [8] => 60s
            [9] => 
            [10] => Audio
        )

    [8] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Adam%20Faith%20-%20Poor%20Me.mp3
            [2] => Adam Faith
            [3] => Poor Me
            [4] => 
            [5] => 
            [6] => 60s
            [7] => 60s
            [8] => 60s
            [9] => 
            [10] => Audio
        )

    [9] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Allisons%20-%20Are%20You%20Sure.mp3
            [2] => Allisons
            [3] => Are You Sure
            [4] => 
            [5] => 
            [6] => 60s
            [7] => 60s
            [8] => 60s
            [9] => 
            [10] => Audio
        )

)

因此,在此示例中,我们可以看到http://streamlink.com/2Pac%20Feat.%20Elton%20John%20-%20Ghetto%20Gospel.mp3显示在00s&amp; 50多岁。然后它应该能够在其完整的信息状态中创建所有项目的新数组。

所以我应该留下:

Array
(
    [0] => Array
        (
            [0] => 
            [1] => http://streamlink.com/2Pac%20Feat.%20Elton%20John%20-%20Ghetto%20Gospel.mp3
            [2] => 2Pac Feat. Elton John
            [3] => Ghetto Gospel
            [4] => 
            [5] => 
            [6] => 00s
            [7] => 50s
            [8] => 50s
            [9] => 
            [10] => Audio
        )

    [1] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Aaliyah%20-%20Try%20Again.mp3
            [2] => Aaliyah
            [3] => Try Again
            [4] => 
            [5] => 
            [6] => 00s
            [7] => 00s
            [8] => 00s
            [9] => 
            [10] => Audio
        )

    [2] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Adele%20-%20Chasing%20Pavements.mp3
            [2] => Adele
            [3] => Chasing Pavements
            [4] => 
            [5] => 
            [6] => 00s
            [7] => 00s
            [8] => 00s
            [9] => 
            [10] => Audio
        )

    [3] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Al%20Hibbler%20-%20Unchained%20Melody.mp3
            [2] => Al Hibbler
            [3] => Unchained Melody
            [4] => 
            [5] => 
            [6] => 50s
            [7] => 50s
            [8] => 50s
            [9] => 
            [10] => Audio
        )

    [4] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Al%20Martino%20-%20Here%20In%20My%20Heart.mp3
            [2] => Al Martino
            [3] => Here In My Heart
            [4] => 
            [5] => 
            [6] => 50s
            [7] => 50s
            [8] => 50s
            [9] => 
            [10] => Audio
        )

    [5] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Alma%20Cogan%20-%20Dreamboat.mp3
            [2] => Alma Cogan
            [3] => Dreamboat
            [4] => 
            [5] => 
            [6] => 50s
            [7] => 50s
            [8] => 50s
            [9] => 
            [10] => Audio
        )

    [6] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Acker%20Bilk%20-%20Stranger%20On%20The%20Shore.mp3
            [2] => Acker Bilk
            [3] => Stranger On The Shore
            [4] => 
            [5] => 
            [6] => 60s
            [7] => 60s
            [8] => 60s
            [9] => 
            [10] => Audio
        )

    [7] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Adam%20Faith%20-%20Poor%20Me.mp3
            [2] => Adam Faith
            [3] => Poor Me
            [4] => 
            [5] => 
            [6] => 60s
            [7] => 60s
            [8] => 60s
            [9] => 
            [10] => Audio
        )

    [8] => Array
        (
            [0] => 
            [1] => http://streamlink.com/Allisons%20-%20Are%20You%20Sure.mp3
            [2] => Allisons
            [3] => Are You Sure
            [4] => 
            [5] => 
            [6] => 60s
            [7] => 60s
            [8] => 60s
            [9] => 
            [10] => Audio
        )

)

请注意,显示http://streamlink.com/2Pac%20Feat.%20Elton%20John%20-%20Ghetto%20Gospel.mp3已合并的数组[0]现在包括Genre1:00s,Genre2:50s&amp; Genre3也是50秒。这就是我希望最终数组包含的方式。

我希望我能解释一下自己,也许有人可以说出一些亮点或指出我正确的方向!谢谢!

1 个答案:

答案 0 :(得分:1)

在我看来,流链接是m3u文件中特定轨道的唯一标识符。所以我将基于我的数组并将其用作结果中的第一级键:

[
    'http://streamlink.com/2Pac%20Feat.%20Elton%20John%20-%20Ghetto%20Gospel.mp3' => [
        [0] => 
        [1] => http://streamlink.com/2Pac%20Feat.%20Elton%20John%20-%20Ghetto%20Gospel.mp3
        [2] => 2Pac Feat. Elton John
        [3] => Ghetto Gospel
        [4] => 
        [5] => 
        [6] => 00s
        [7] => 00s
        [8] => 00s
        [9] => 
        [10] => Audio
]

等。这样您就可以轻松匹配同一个文件并添加标签。如果您需要摆脱最后的键以获得正常的数组,您可以在其上运行array_values