dongyejun1983
2014-01-06 20:10
浏览 29
已采纳

PHP Simple Dom - 一次循环一个班级?

Is there any way to do this? currently I'm using this method to go through the td's for every table in a wikipedia entry that have a class of wikitable.

foreach ($html->find('table.wikitable td') as $key => $info)
{
    $text = $info->innertext;
}

However, what I want to do is have seperate loops for each table that share the class name of wikitable. I can't figure out how to do this.

Is there some kind of syntax? I want to do something like this

$first_table = $html->find('table.wikitable td', 0); // return all the td's for the first table
$second_table = $html->find('table.wikitable td', 1); // second one

图片转代码服务由CSDN问答提供 功能建议

有没有办法做到这一点? 目前我正在使用这种方法来浏览wikipedia条目中具有wikitable类的每个表的td。

  foreach($ html-> find('  table.wikitable td')as $ key => $ info)
 {
 $ text = $ info-> innertext; 
} 
   
 
 

但是,我想要做的是为每个共享类wikitable的表分配单独的循环。 我无法弄清楚如何做到这一点。

是否有某种语法? 我想做这样的事情

  $ first_table = $ html-> find('table.wikitable td',0);  //返回第一个表的所有td 
 $ second_table = $ html-> find('table.wikitable td',1);  //第二个
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjiwu0324 2014-01-06 20:17
    已采纳

    I might not fully understand your question but it seems that $html->find simply returns an array, in your case an array of tables:

    $tables = $html->find('table.wikitable');
    

    You can then loop through your tables and find the td's in each table:

    foreach( $tables as $table )
    {
       $tds = $table->find('td');
    
       foreach( $tds as $td )
       {
          ...
       }
    }
    

    If you only want to target the second table you can use:

    $table = $tables[1];
    

    Or something like that.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题