Search code examples
phpscreen-scrapingsimple-html-dom

Retrieve data from the first td in every tr


I'm scraping a page which contains of a table with several tr's. Inside every tr there's four td's, and I want to get the data from the first of these td's. Below is the code I've tried so far, but it grabs all the td's. How can I accomplish what I want?

...
$html = new simple_html_dom();
$html = file_get_html($url);

foreach($html->find('table tr') as $row) {
    foreach($row->find('td', 0) as $cell) {
        echo $cell;
    }
}

Solution

  • Think about why you're using the second foreach when you actually only mean to act on one element within each row.

    $html = new simple_html_dom();
    $html = file_get_html($url);
    
    foreach($html->find('table tr') as $row) {
        $cell = $row->find('td', 0);
        echo $cell;
    }