JBithell
JBithell

Reputation: 627

For Loop - Return first 2 instead of all

<?php
require 'simple_html_dom.php';
$html = file_get_html("website" . date("Ymd")); 

foreach($html->find('td[class=x]') as $element) 
       echo $element;
?>

I am using the above code to parse a website. Instead of returning all the td elements I would like to return the first two. I think I would need to edit the for loop. How can I do this. I have limited PHP experience.

Upvotes: 0

Views: 38

Answers (1)

codenathan
codenathan

Reputation: 774

One technique would be to use a counter

   $counter = 0;
    foreach ($html->find('td[class=x]') as $element) {
        if($counter<=1){
            echo $element;
        }
        $counter++;
    }

Upvotes: 1

Related Questions