简体   繁体   中英

Printing all values in array

I have the following code that prints out the first value in the array (Twitter post):

$connection = new TwitterOAuth($twitter_customer_key, $twitter_customer_secret, $twitter_access_token, $twitter_access_token_secret);

$my_tweets = $connection->get('statuses/user_timeline', array('screen_name' => 'xxx', 'count' => 10));

echo '<div class="tweets">';
if(isset($my_tweets->errors))
{           
echo 'Error :'. $my_tweets->errors[0]->code. ' - '. $my_tweets->errors[0]->message;
}else{
echo makeClickableLinks($my_tweets[0]    ->text);
}
echo '</div>';

However, I want to print out all values in the array. I have tried using below method as stated here: http://php.net/manual/en/function.array-values.php

array_values($my_tweets)

But that returns the following: "Trying to get property of non-object"

What exactly am I doing wrong?

Thank you.

Try this

    for($i = 0; $i<=count($my_tweets) -1; $i++){
        if(isset($my_tweets[$i]->errors)){                   
            echo 'Error :'. $my_tweets->errors[$i]->code. ' - '. $my_tweets->errors[$i]->message;
        } else{
            echo makeClickableLinks($my_tweets[$i]->text);
        }
    }
var_dump($my_tweets);

OR

print_r($my_tweets);

You can also loop

foreach($my_tweets as $tweet){
    echo $tweet;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM