0

I am using an api for film dates and im trying to parse a json array. Ive tried to parse the release dates but i receive this error - Fatal error: Cannot use string offset as an array

Below is an example from the array

Array
(
    [total] => 17
    [movies] => Array
        (
            [0] => Array
                (
                    [id] => 22494
                    [title] => Titanic (in 3D)
                    [year] => 1997
                    [mpaa_rating] => PG-13
                    [runtime] => 195
                    [critics_consensus] => A mostly unqualified triumph for Cameron, who offers a dizzying blend of spectacular visuals and old-fashioned melodrama.
                    [release_dates] => Array
                        (
                            [theater] => 2012-04-04
                            [dvd] => 1999-08-31
                        )

Here's my simple code that is receiving the error.

<?php
$url = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/upcoming.json?apikey=px8rr7zr5c6qjwpea66gdf93&page_limit=18';
$json = file_get_contents($url);
$data = json_decode($json, TRUE);

foreach($json['releasedates']['theatre'] as $item) {
    print $item['theatre'];
}

?>

Ideally i want to parse the dates into a variable and be able to compare them to the current day

Thanks for your help guys :)

1 Answer 1

2

Your foreach is using the wrong variable (and you spelled theater wrong, per the data)

EDIT: Your script, working:

<?php
    $url = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/upcoming.json?apikey=px8rr7zr5c6qjwpea66gdf93&page_limit=18';
    $json = file_get_contents($url);
    $data = json_decode($json, TRUE);

    foreach($data['movies'] as $item) {
        echo $item['title'] . ' is opening on ' . $item['release_dates']['theater'] . "\n";
    }
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't work with the data provided. $json['releasedates'] is an array containing one element, "2011-07-29", this has a key of "theater".
I just edited and posted a working script ... the data he posted does not match what is being returned from the url in the script he provided ...
Thanks for your answer, its perfect. How would i go about analyzing the date returned so that it would show the difference between the dates. For example titanic is being released 4 days before lord of the rings? Should i post a separate question?
That's just a matter of storing the variables and doing date math to compute the differences. Give it a shot and, if you get stuck, we'll be here to help ...

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.