Php 如何循环通过此阵列以获得所需的内容?

Php 如何循环通过此阵列以获得所需的内容?,php,arrays,Php,Arrays,我正在使用twitter API检索我所有的推文。但是,我似乎无法获得“扩展url”和“hashtag”属性。有关此特定API的文档,请访问。我的代码如下: $retweets = 'http://api.twitter.com/1/statuses/user_timeline.json? include_entities=true&include_rts=true&screen_name=callmedan'; $ch = curl_init(); curl_setopt($

我正在使用twitter API检索我所有的推文。但是,我似乎无法获得“扩展url”和“hashtag”属性。有关此特定API的文档,请访问。我的代码如下:

$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 />";

}
$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\u exec($ch);
卷曲关闭($ch);
$response=json_decode($curlout,true);
$tweet_number=计数($response);
对于($i=0;$i<$tweet_number;$i++)
{
$url=$response['entities']['url'];
$hashtag=$response['entities']['hashtags'];
$text=$response[$i]['text'];
回显“$url
”; echo“$hashtag
”; 回显“$text
”; 回声“

”; }
我收到一条错误消息,上面写着“注意:未定义的索引:实体”

有什么建议吗?

您应该这样做(如果$response是一个数组,您必须访问适当的索引):

否则,请使用foreach:

foreach ($response as $r){
    $url = $r['entities']['urls'];
    $hashtag = $r['entities']['hashtags'];
    $text = $r['text'];

您正在为循环使用递增的整数,但没有使用
$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 />";

}
foreach($tweet响应)
{
$url=$tweet['entities']['url'];
$hashtag=$tweet['entities']['hashtags'];
$text=$tweet['text'];
回显“$url
”; echo“$hashtag
”; 回显“$text
”; 回声“

”; }
echo var_dump($response)
并确保您正在访问您认为在其中的数据(确保数组中存在索引)。这也是一种很好的方式,可以直观地看到您试图检索的内容。
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 />";

}