PHP Simple Dom - Loop through one class at a time?

151 views Asked by At

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

There are 1 answers

0
Luke On BEST ANSWER

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.