2

This is a stupid easy question for some of you, but i can't seem to get a result printed out.

I keep getting Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\td.php on line 14

Thanks

$url = "http://search.twitter.com/search.json?q=shoes&include_entities=true&rpp=2";

$data = file_get_contents($url);
$result = json_decode($data, true);

foreach ($result->results as $data) {
    echo $data->created_at;
}

The json return looks like this.

Array
(
    [completed_in] => 0.019
    [max_id] => 1.4886632421341E+17
    [max_id_str] => 148866324213411841
    [next_page] => ?page=2&max_id=148866324213411841&q=shoes&rpp=2&include_entities=1
    [page] => 1
    [query] => shoes
    [refresh_url] => ?since_id=148866324213411841&q=shoes&include_entities=1
    [results] => Array
        (
            [0] => Array
                (
                    [created_at] => Mon, 19 Dec 2011 20:44:32 +0000
    ...
    etc
    ...

1 Answer 1

3

$result is an array but you're treating it like an object. Likewise, its inner component $results is also an array of associative arrays, but you've treated it as an object. $data in context of your loop is an array having created_at as one of its keys:

// Access [results] as an array key
foreach ($result['results'] as $data) {
    // $data is $results[0], $results[1], ...,$results[n]
    // [created_at] is an array key of $results[n]
    echo $data['created_at'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yup that was that. I tried $result['results'] but didnt' also change to $data['created_at']. Thanks!

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.