jord49 jord49 - 7 months ago 10
PHP Question

Change foreach loop to a for loop

I'm quite new to PHP. How can I change this foreach loop to only loop twice?

<?php

foreach($results as $row):

?>

Answer

Try this code, you just need a counter variable, initialize your counter variable from zero and increment it after each iteration.

<?php 
    $counter = 0; 
    foreach($results as $row) {
         //your code
         if($counter == 1)
         {
              break;
         }
         $counter++;   
    }

    ?>