You'er searching the whole document for cultivar
with your second foreach()
loop, not simply the genus
that you're already found with the first foreach()
; and similarly for species
within cultivar
But because each entry is actually in its own row in a nested table, you also need to backtrack up through the dom for each entry:
foreach($html->find('td.genus') as $a)
foreach($a->parent()->parent()->find('td.cultivar') as $b)
foreach($b->parent()->parent()->find('td.species') as $c)
Better variable naming might also make this clearer:
foreach($html->find('td.genus') as $genus) {
foreach($genus->parent()->parent()->find('td.cultivar') as $cultivar) {
foreach($cultivar->parent()->parent()->find('td.species') as $species) {
echo '<tr><td>' .
$genus->innertext .
'</td><td>' .
$cultivar->innertext .
'</td><td>' .
$species->innertext.
'</td></tr>';
}
}
}