/ / Come posso eseguire il loop su questo array per ottenere ciò di cui ho bisogno? - php, matrici

Come posso eseguire il loop su questo array per ottenere ciò di cui ho bisogno? - php, matrici

Sto lavorando con l'API di Twitter per recuperare tutti i miei tweet, ma non riesco a ottenere le proprietà "expanded_url" e "hashtag". La documentazione per questa particolare API è disponibile all'indirizzo https://dev.twitter.com/docs/api/1/get/statuses/user_timeline. Il mio codice è il seguente:

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

}

Viene visualizzato un messaggio di errore "Avviso: indice non definito: entità".

Eventuali suggerimenti?

risposte:

0 per risposta № 1

Dovresti fare (se $ response è un array devi accedere all'indice corretto):

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

Altrimenti usa foreach:

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

0 per risposta № 2

Stai usando un ciclo incrementato per intero, ma non utilizzando il $i indice. Invece, usa 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 />";

}