[英]PHP foreach loop not working properly - dies after half array iteration
在此脚本中,我正在加载一个包含80个项目的URL。 在simple_html_dom的帮助下,对总计80的每个项目“ tr”进行迭代。
但是在下面的代码中,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,这是同样的问题(它还表明没有超时问题)
所有项目都是相同的,具有相同的类型和相同数量的元素。
似乎缺少一个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);
将其放在文件开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.