-2

Possible Duplicate:
limiting number of times a loop runs in php

I am trying to break a foreach loop reading a feed of tweets - after three tweets. Would somebody help with the code I need to supplement to the one i have already.

<?php
function getTweets($Username) {
$feedURL = "http://twitter.com/statuses/user_timeline.rss?screen_name=" . $username;

$content = file_get_content($feedURL);
$tweets = new SimpleXMLElement ($content);

foreach ($tweets->channel->item as $tweet) {
    echo "<ul>";
    echo "<li>$tweet->description<br />$tweet->pubDate</li>";
    echo "</ul>";
}
?>
}

Thanking you in advance.

4

3 回答 3

2

大概是这样的。

$max_count = 3;
$counter = 0;
foreach ($tweets->channel->item as $tweet) {
    echo "<ul>";
    echo "<li>$tweet->description<br />$tweet->pubDate</li>";
    echo "</ul>";
    if($counter == $max_count){
        break;
    }
    $counter++;
}
于 2012-08-31T07:36:37.717 回答
1

您可以使用休息

foreach ($tweets->channel->item as $tweet) {
    echo "<ul>";
    echo "<li>$tweet->description<br />$tweet->pubDate</li>";
    echo "</ul>";
    if( someCondition )
    {
        break;
    }
}
于 2012-08-31T07:36:19.567 回答
1

我假设项目已编入索引:

foreach ($tweets->channel->item as $i => $tweet) {
    echo "<ul>";
    echo "<li>$tweet->description<br />$tweet->pubDate</li>";
    echo "</ul>";
    if ($i == 2) {
        break;
    }
}
于 2012-08-31T07:37:29.027 回答