29

Here is the loop.

foreach($results->results as $result){
    echo '<div id="twitter_status">';
    echo '<img src="'.$result->profile_image_url.'" class="twitter_image">';
    $text_n = $result->text; 
    echo "<div id='text_twit'>".$text_n."</div>";
    echo '<div id="twitter_small">';
    echo "<span id='link_user'".'<a href="http://www.twitter.com/'.$result->from_user.'">'.$result->from_user.'</a></span>';
    $date = $result->created_at;

    $dateFormat = new DateIntervalFormat();

    $time = strtotime($result->created_at);

    echo "<div class='time'>";

    print sprintf('Submitted %s ago',  $dateFormat->getInterval($time));

    echo '</div>';

    echo "</div>";
    echo "</div>";
user229044
  • 232,980
  • 40
  • 330
  • 338
Tapha
  • 1,491
  • 3
  • 17
  • 32

4 Answers4

72

With the break command.

You are missing a bracket though.

$i=0;
foreach($results->results as $result){
//whatever you want to do here

$i++;
if($i==3) break;
}

More info about the break command at: http://php.net/manual/en/control-structures.break.php

Update: As Kyle pointed out, if you want to break the loop it's better to use for rather than foreach. Basically you have more control of the flow and you gain readability. Note that you can only do this if the elements in the array are contiguous and indexable (as Colonel Sponsz pointed out)

The code would be:

for($i=0;$i<3;$i++){
$result = $results->results[i];
//whatever you want to do here
}

It's cleaner, it's more bug-proof (the control variables are all inside the for statement), and just reading it you know how many times it will be executed. break / continue should be avoided if possible.

Community
  • 1
  • 1
pakore
  • 11,395
  • 12
  • 43
  • 62
  • 1
    ...or simply if(++$i==3) break; – Manos Dilaverakis May 19 '10 at 12:36
  • 1
    No! Don't use breaks, just use a for loop. – Kyle May 19 '10 at 12:57
  • 2
    @Manos Dilaverakis: that would work, yes, but the goal of the answer is to understand how "break" works, not to do eager optimization. – pakore May 19 '10 at 13:07
  • 4
    @pakore: Absolutely. This is the exactly kind of "smartness" that causes obscure bugs and hard-to-understand code while providing not benefit *at all* in return. – Tomalak May 19 '10 at 13:33
  • The updated for loop is not the right answer if the array doesn't have contiguous numeric indexes starting at 0. If you need a solution that can accept any array then the first answer is best. – Colonel Sponsz May 19 '10 at 14:01
  • @Colonel Sponsz , you're right, i've edited the response. This is one of the reasons I love Java iterators :D. – pakore May 19 '10 at 14:28
  • thanks as my question was different but got help from your post – smehsoud Oct 04 '16 at 15:23
7
  • Declare a variable before the loop, initialize to 0.
  • Increment variable at the beginning of the body of for-each.
  • Check variable at the end of the body of for-each.
    • If it's 3, break.

You have to be careful with this technique because there could be other break/continue in the for-each body, but in your case there isn't, so this would work.

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • I have had problems trying this. As i am dealing with an object. As soon as i place a variable onto the key value then iterate my object loses its keys. Weird i know. – Tapha May 19 '10 at 12:28
3

Increment some counter $i at the beggining of the loop and break; when it reaches 3, e.g.:

if ($i++ == 3)
    break;
Chris Schmich
  • 29,128
  • 5
  • 77
  • 94
  • 3
    Correct would be `if ++i == 3` and *not* incrementing the counter elsewhere in the loop. It's neither smart nor advisable to use prefix/postfix increment operator in conjunction with value comparison, because it leads to exactly these kinds of obscure and completely avoidable bugs. – Tomalak May 19 '10 at 12:39
  • Agreed on the readability point, but putting `if ($i++ == 3) break;` at the beginning of the loop (as I mentioned) *would* work. – Chris Schmich May 19 '10 at 12:45
  • Yes, it appears to stop the loop after three iterations (in fact, it runs 4 times, only that the 4th iteration is broken out of right away), but it also counts up $i too early and you could not run a loop starting from 0 (try it). Say what you want, this approach is fundamentally flawed. (In a `foreach` loop, these implications won't matter too much, but it's best not to get into this coding style at all.) – Tomalak May 19 '10 at 13:10
2
foreach($results->results as $i => $result){ 
   if($i==3) break; 
   //whatever you want to do here 
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • 2
    This makes a wrong assumption about the type of array - it will break as soon as the array does not use numerical keys that go from 0..n-1. – Tomalak May 19 '10 at 13:29
  • @Tomalak I find this pattern quite safe with PHP when looping over the results of a query. Generally you're going to have a clear distinction between whether an array is a traditional array or one with non-numeric or non-continuous keys. – user229044 May 19 '10 at 13:42
  • Yes, if you know that you're dealing with a "standard" numerical array this is okay. – Tomalak May 19 '10 at 16:46