php foreach循环嵌套增加重复

时间:2015-01-21 08:05:18

标签: php foreach nested-loops

我有一个嵌套在循环中的循环。所有数据都显示正常,直到我进入第二个循环并且返回的数据不断重复。它达到了第二个循环中的项目的歌曲重复超过15次的程度。

项 -subitems

项 -subitems -subitems

项 -subitems -subitems -subitems

项 -subitems -subitems -subitems -subitems

我被告知清除循环,但我确定如何做到这一点。有任何想法吗? 如果需要,这是一个实时示例:view-source:http://mixtapemonkey.com/json

<?php 
include ("connection.php");

foreach($db->query("SELECT * FROM mixtapes WHERE year='2014' OR year = '2015' ORDER BY id DESC LIMIT 24") as $info) {

    echo "mixtape-".$info['id']," {\n";
        echo "\t\"id\" : \"".$info['id']."\",\n";
        echo "\t\"title\" : \"".$info['title']."\",\n";
        echo "\t\"name\" : \"".$info['name']."\",\n";
        echo "\t\"genre\" : \"".$info['genre']."\",\n";
        echo "\t\"year\" : \"".$info['year']."\",\n";
        echo "\t\"downloads\" : \"".$info['downloads']." Downloads\",\n";
        echo "\t\"thumb\" : \"http://mixtapemonkey.com/".$info['thumb']."\",\n";
        echo "\t\"songs\" : {\n";

            if ($handle = opendir("mixtapes/zip/".$info['id'])) {
                while (false !== ($entry = readdir($handle))) {
                    if ($entry != "mixtapes/zip/".$info['id'] && $entry != "mixtapes/zip/".$info['id']) {   


                            if ($newhandle = opendir("mixtapes/zip/".$info['id']."/".$entry)) {
                                while (false !== ($newentry = readdir($newhandle))) {
                                    if ($newentry != "mixtapes/zip/".$info['id']."/".$entry && $newentry != "mixtapes/zip/".$info['id']."/".$entry) {

                                       $ext = substr($newentry, strrpos($newentry, '.') + 1);
                                       if ($ext == "mp3" || $ext == "m4a")
                                        {
                                            $item = "mixtapes/zip/".$info['id']."/".$entry."/";
                                            $musicarray[] = $newentry;
                                            sort($musicarray);
                                        }
                                    }
                                }
                                closedir($newhandle);
                            }

                    }
                }
                closedir($handle);
            }
            foreach ($musicarray as $song) {

            echo "\t\t\"track\" : \"http://mixtapemonkey.com/$item$song\",\n";
            }

        echo "\t}\n";
    echo "},\n\n";
}?> 

1 个答案:

答案 0 :(得分:0)

我也有同样的问题,我解决了使用php break after循环。所以你也可以尝试一下。

break;