Search code examples
phpforeachhtml-tablefindsimple-html-dom

How I only get the first TDs of each TR, with Simple Html Dom?


I need to get only the first columns of all rows from a table using Simple Html Dom. How I do this?

foreach($html->find('table.horarios-filmes-single tr') as $tr) {

    foreach ($tr->find('td', 1) as $element) {
       echo $element->plaintext . '<br>';
    }
}

This returns the error:

enter image description here


Solution

  • This should help you with your problem,

    $e = []; 
    foreach($html->find('table.horarios-filmes-single tr') as $tr) {
        $e[] = $tr->find('td', 0); 
    }
    

    0 index will give you first instance of td in every tr

    If still face any problem, you should print_r($html->find('table.horarios-filmes-single tr')); and check if you are getting any data.