简体   繁体   English

PHP foreach循环无法正常工作-半数组迭代后死亡

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

In this script i am loading an URL, that has 80 items. 在此脚本中,我正在加载一个包含80个项目的URL。 With the help of simple_html_dom iterating for each item 'tr' which is total of 80. 在simple_html_dom的帮助下,对总计80的每个项目“ tr”进行迭代。

But foreach loop iterating only 42 items in the following code. 但是在下面的代码中,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);
?>

On removing few lines from the loop it is iterating complete to 81. 从循环中删除几行后,它就迭代完成到81。

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

I don't know what's going wrong. 我不知道怎么了。 It already took my day. 我已经花了很多时间。

It is not issue of timeout because i used set_time_limit(0); 这不是超时的问题,因为我使用了set_time_limit(0); not working. 不工作。

If the number of items "tr" redured to 40 then the loop is iterating to 21 again same problem (it also tells there is no issue of timeout) 如果项目“ tr”的数量减少到40,则循环再次迭代到21,这是同样的问题(它还表明没有超时问题)

All items are identical, have same types and same number of elements. 所有项目都是相同的,具有相同的类型和相同数量的元素。

One td is missing from html, it seems, so: 似乎缺少一个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);

Turn errors on: 打开错误:

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

Put that right at the beginning of your file. 将其放在文件开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM