/ / Ako môžem slučiť cez toto pole, aby som získal to, čo potrebujem? - php, polia

Ako môžem prechádzať cez toto pole, aby som získal to, čo potrebujem? php, polia

Pracujem s programom Twitter API, aby som získal všetky moje tweety. Zdá sa však, že nemám vlastnosti „expand_url“ a „hashtag“. Dokumentáciu k tomuto konkrétnemu API nájdete na https://dev.twitter.com/docs/api/1/get/statuses/user_timeline, Môj kód je nasledovný:

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

}

Zobrazuje sa mi chybové hlásenie „Upozornenie: Nedefinovaný index: entity“.

Nejaké návrhy?

odpovede:

0 pre odpoveď č. 1

Mali by ste to urobiť (ak $ response je pole, musíte mať prístup k správnemu indexu):

$url = $response[$i]["entities"]["urls"];
$hashtag = $response[$i]["entities"]["hashtags"];
$text = $response[$i]["text"];

Inak použite foreach:

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

0 pre odpoveď č. 2

Pre slučku používate celočíselný prírastok, ale nevyužívate $i index. Namiesto toho použite a 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 />";

}