[英]How can I loop thru this array to get what I need?
我正在使用 twitter API 來檢索我的所有推文。 但是,我似乎無法獲得“expanded_url”和“hashtag”屬性。 此特定 API 的文檔可在https://dev.twitter.com/docs/api/1/get/statuses/user_timeline找到。 我的代碼如下:
$retweets = 'http://api.twitter.com/1/statuses/user_timeline.json? include_entities=true&include_rts=true&screen_name=callmedan';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $retweets);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
$tweet_number = count($response);
for($i = 0;$i < $tweet_number;$i++)
{
$url = $response['entities']['urls'];
$hashtag = $response['entities']['hashtags'];
$text = $response[$i]['text'];
echo "$url <br />";
echo "$hashtag <br />";
echo "$text <br />";
echo "<br /><br />";
}
我收到一條錯誤消息,內容為“注意:未定義的索引:實體。”
有什么建議么?
你應該這樣做(如果 $response 是一個數組,你必須訪問正確的索引):
$url = $response[$i]['entities']['urls'];
$hashtag = $response[$i]['entities']['hashtags'];
$text = $response[$i]['text'];
否則使用 foreach:
foreach ($response as $r){
$url = $r['entities']['urls'];
$hashtag = $r['entities']['hashtags'];
$text = $r['text'];
您正在使用整數遞增的 for 循環,但沒有使用$i
索引。 相反,使用foreach
:
foreach($response as $tweet)
{
$url = $tweet['entities']['urls'];
$hashtag = $tweet['entities']['hashtags'];
$text = $tweet['text'];
echo "$url <br />";
echo "$hashtag <br />";
echo "$text <br />";
echo "<br /><br />";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.