4

I'm pulling in a search from the Twitter api, fetching the data with file_get_contents and then passing to json_decode which give me this array structure.

{"results":[
     {
     "from_user":"A-user-name",
     "from_user_id":457304735,
     "text":"Ich R U #BoysNoize #SuperRola",

          "entities":{

                 "urls":[{
                        "url":"http:\/\/t.co\/WZnUf68j",
                        "expanded_url":"http:\/\/instagr.am\/p\/Vz4Nnbnjd6\/",
                        }]

     }]
]

This repeats for every tweet pulled, Now I can access the user name and text using a foreach loop and assigning every instance of results to a variable, then pulling data from the variable.

foreach($jsonArr['results'] as $item){    

// Takes the Array jsonArr and for every results heading creates an $item

    $user = mysql_real_escape_string($item['from_user']);
    $text = mysql_real_escape_string($item['text']);

This saves the correct variables OK, But I can't seem to get the the data within the entities array within the results. If I print out the Entities var like the username or text I get

ArrayArrayArrayArrayArrayArrayArrayArrayArrayArray

So It holds the arrays for each results returned but how on earth do I access it, I've been messing around with a few other methods I know for accessing array data but they all seem to fall flat. Any help with how to get at these values, or integrate them with the foreach would be greatly appreciated

1
  • Just print_r($jsonArr); to see it's exact structure. I don't see any username or text in entities you posted. Commented Feb 18, 2013 at 11:10

3 Answers 3

4

Assuming that you've chosen to decode the JSON as a multi-dimensional array, rather than as objects:

foreach ($results as $tweet) {
    $user = $tweet["from-user"];
    $text = $tweet["text"];

    $entities = $tweet["enities"];
    $urls = $entities["urls"];

    foreach ($urls as $url) {
        echo $url["expanded_url"];
    }
}

et cetera

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, between yourself and @Akam I was able to get it working, there was just another level to navigate with your solution. $urls = $entities["urls"][0];
4
Array
(
    [results] => Array
        (
            [0] => stdClass Object
                (
                    [entities] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [urls] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [expanded_url] => http://instagr.am/p/Vz4Nnbnjd6/
                                                    [url] => http://t.co/WZnUf68j
                                                )

                                        )

                                )

                        )

                    [from_user] => A-user-name
                    [from_user_id] => 457304735
                    [text] => Ich R U #BoysNoize #SuperRola
                )

        )

)

Accessing url:

$json_array['results'][0]->entities[0]->urls[0]->url;

Helpful code:

<?php

$json ='{ "results" : [ { "entities" : 
[ { "urls" : [ { "expanded_url" : "http://instagr.am/p/Vz4Nnbnjd6/",
                    "url" : "http://t.co/WZnUf68j"
                  } ] } ],
        "from_user" : "A-user-name",
        "from_user_id" : 457304735,
        "text" : "Ich R U #BoysNoize #SuperRola"
      } ] }';
$json_array = (array)(json_decode($json));
echo '<pre>';
 //print_r($json_array);

 echo $json_array['results'][0]->entities[0]->urls[0]->url;

?>

1 Comment

Thanks for the layout of how to navigate within a multidimensional array, between that and you example it allowed me to solve my problem and learn something.
1

Just do a print_r($jsonArr); and you will be able to work with your decoded json.

1 Comment

While this might be good advice to help understand the data, this does not solve the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.