Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我在foreach语句中使用foreach,但值是重复的_Php - Fatal编程技术网

Php 我在foreach语句中使用foreach,但值是重复的

Php 我在foreach语句中使用foreach,但值是重复的,php,Php,这是麦可德 foreach($html->find('ul.listings li a') as $tvshow) $tvshows=preg_replace('/<span[^>]*>([\s\S]*?)<\/span[^>]*>/', '', $tvshow->innertext); foreach($html->find('li a span.epnum') as $e) $query=mysql_query("ins

这是麦可德

foreach($html->find('ul.listings li a') as $tvshow)
    $tvshows=preg_replace('/<span[^>]*>([\s\S]*?)<\/span[^>]*>/', '', $tvshow->innertext);
foreach($html->find('li a span.epnum') as $e)

    $query=mysql_query("insert into tvshow(count,author,tvshowname,imdblink,tvshowlink,description,year,image,imagetype,website,date,rating) 
              values('$count','$author','$tvshows','$imdblink','$tvshow->href','$tvshowdescription','$e->innertext','$tvshowname','$tvshowtype','$website','$date','$rating')");
    if(!$query)
    {
        die(mysql_error());
    }
foreach($html->find('ul.a')作为$tvshow)
$tvshows=preg_replace('/]*>([\s\s]*?)]*>/','''$tvshow->innertext);
foreach($html->find('li a span.epnum')作为$e)
$query=mysql\u query(“插入到tvshow中(计数、作者、tvshowname、imdblink、tvshowlink、说明、年份、图像、图像类型、网站、日期、评级)
值(“$count”、“$author”、“$tvshows”、“$imdblink”、“$tvshow->href”、“$tvshowdescription”、“$e->innertext”、“$tvshowname”、“$tvshowtype”、“$website”、“$date”、“$rating”);
如果(!$query)
{
die(mysql_error());
}

这里$tvshows,$tvshow->href正在重复,我不希望它们重复。不要担心代码一切都是正确的,只使用两个foreach语句会重复。请不要重复代码。

花括号的力量

foreach($html->find('ul.listings li a') as $tvshow) {
    $tvshows=preg_replace('/<span[^>]*>([\s\S]*?)<\/span[^>]*>/', '', $tvshow->innertext);

    foreach($html->find('li a span.epnum') as $e) {

        $query=mysql_query("insert into tvshow(count,author,tvshowname,imdblink,tvshowlink,description,year,image,imagetype,website,date,rating) 
                  values('$count','$author','$tvshows','$imdblink','$tvshow->href','$tvshowdescription','$e->innertext','$tvshowname','$tvshowtype','$website','$date','$rating')");
        if(!$query)
        {
            die(mysql_error());
        }
    }
}

有道理吗?

你能再解释一下吗?我不明白你在问什么。我已经编辑过了,请检查一下,这是你代码的直接剪辑吗?从该片段中,第2行位于顶部foreach的内部,第二个foreach位于外部,只有第5行应用于它。用{}包装应用代码,这样foreach就封装了正确的代码。你能解释清楚吗?Flosculus和pls如果我使用卷曲的brraces,每个值重复5次,那么我不确定你在寻找什么样的答案。你的问题标题是我在foreach语句内部使用foreach,但是值是重复的,所以我认为问题在于你的foreach循环实际上并不在彼此内部。如果这不能解决您的问题,那么答案就在代码的其他地方。有趣的是,您提供的代码示例让我想知道您可能还犯了哪些语法上的错误。我的问题已经解决了,Tq jdstankosky感谢您试图帮助我。以下是我如何解决的代码。以下是代码
code
foreach($html->find('ul.a')as$tvshow){$tvshows=preg_replace('/]*>([\s\s]*?)]*>/“,”,“$tvshow->innertext);$query=mysql\u query(“插入到tvshow(计数、作者、tvshowname、imdblink、tvshowlink、说明、年份、图像、图像类型、网站、日期、评级)值(“$count”、“$author”、“$tvshow”、“$tvshowdescription”、“$tvshowname”、“$tvshowtype”、“网站”、“日期”、“评级”);设置时间限制(10000); }
foreach($html->find('ul.listings li a') as $tvshow) 
    $tvshows=preg_replace('/<span[^>]*>([\s\S]*?)<\/span[^>]*>/', '', $tvshow->innertext);
    foreach($html->find('li a span.epnum') as $e) 

        $query=mysql_query("insert into tvshow(count,author,tvshowname,imdblink,tvshowlink,description,year,image,imagetype,website,date,rating) 
                  values('$count','$author','$tvshows','$imdblink','$tvshow->href','$tvshowdescription','$e->innertext','$tvshowname','$tvshowtype','$website','$date','$rating')");
        if(!$query)
        {
            die(mysql_error());
        }