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

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
  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?