PHP foreach循环不能正常工作-在数组迭代一半后死亡


PHP foreach loop not working properly - dies after half array iteration

在这个脚本中,我正在加载一个URL,它有80个项目。在simple_html_dom的帮助下,迭代每个项目'tr',总共80。

但是在下面的代码中,foreach循环只迭代42个项目。

<?php
include_once "simple_html_dom.php";
$job_links=array();
$main_url = "http://xyz.com/rescnt=80";
$html = new simple_html_dom();
$html->load_file($main_url);
$fun = $html->find('div[class=dontent_wrap]',0)->find('table',0);
$i=0;
echo count($fun->find('tr'));
foreach($fun->find('tr') as $tr){
    echo ++$i;
    $td = $tr->find( 'td',1);
    $a =  $td->find('a',0);
    $link = $a->href;
    $id = $a->id;
        $id = trim(preg_replace('/link/','',$id)); 
     $my_link ="http://xyz.com/details/".$id.".html";
    if(strpos($link, $my_link)!==false){
        $job_links[] =trim($my_link);
    }
}
echo 'count:'.count($job_links);
print_r($job_links);
?>

如果从循环中删除几行,它将迭代到81。

foreach($fun->find('tr') as $tr){
    echo ++$i;
    $td = $tr->find( 'td',1);
}

我不知道怎么了。我已经花了一天的时间了。

这不是超时的问题,因为我使用的set_time_limit(0);不工作。

如果项数"tr"减少到40,则循环再次迭代到21,同样的问题(它也告诉没有超时问题)

所有条目相同,具有相同类型和相同数量的元素

html中似乎缺少一个td,所以:

 include("simple_html_dom.php");
$job_links=array();
$monster_main_url = "http://jobsearch.monsterindia.com/searchresult.html?day=1&res_cnt=80";
$html = new simple_html_dom();
$html->load_file($monster_main_url);
$fun = $html->find('div[class=dd_content_wrap]',0)->find('table',0);
$i=0;
echo count($fun->find('tr'));
foreach($fun->find('tr') as $tr){
    echo ++$i;

    $td = $tr->find( 'td',1);
    if($td!=NULL) {
    $a =  $td->find('a',0);

    $link = $a->href;
    $id = $a->id;
        $id = trim(preg_replace('/link/','',$id)); 
     $my_link ="http://jobs.monsterindia.com/details/".$id.".html";
    }
    else {
        $my_link="no link";
    }
    if(strpos($link, $my_link)!==false){
        $job_links[] =trim($my_link);
    }
}
echo '<br>count:'.count($job_links);
print_r($job_links);

打开错误:

ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);

把它放在文件的开头