-1

Sorry I am very new in leaning PHP. I have tried to seach this topic, but did not get the answer, hope some one can help me.

I try to display from API Json from Rottentomatoes, If Rottentomatoes Json Output :

 {"total":109,"reviews":[{"critic":"Andrea Chase","date":"2014-07-25","original_score":"2/5","freshness":"rotten","publication":"Killer Movie Reviews","quote":"succeeds only as an advertisement for the tablet mentioned every dozen words or so, and that forms the McGuffin chased with sadly ineffectual diligence by Segal and Diaz.","links":{"review":"hxxp://www.killermoviereviews.com/main.php?nextlink=display&dId=1818&subLinks="}}]

How i display "Andrea Chase" value it in php?

Right Now i susses to display "109" from "total" value, here is my code

e$url1   = 'hxxp://my-url-api-link';
$data = json_decode(file_get_contents($url1));
$total = $data->total;

echo $total;

1 Answer 1

1

For the sake of this ansswer I put your JSON into a variable:

$json = '{"total":109,"reviews":[{"critic":"Andrea Chase","date":"2014-07-25","original_score":"2/5","freshness":"rotten","publication":"Killer Movie Reviews","quote":"succeeds only as an advertisement for the tablet mentioned every dozen words or so, and that forms the McGuffin chased with sadly ineffectual diligence by Segal and Diaz.","links":{"review":"hxxp://www.killermoviereviews.com/main.php?nextlink=display&dId=1818&subLinks="}}]}';

Now if you do this:

echo "<pre>";
$data = json_decode($json);
print_r($data);

You will get the Standard Object of PHP that holds the JSON:

stdClass Object
(
    [total] => 109
    [reviews] => Array
        (
            [0] => stdClass Object
                (
                    [critic] => Andrea Chase
                    [date] => 2014-07-25
                    [original_score] => 2/5
                    [freshness] => rotten
                    [publication] => Killer Movie Reviews
                    [quote] => succeeds only as an advertisement for the tablet mentioned every dozen words or so, and that forms the McGuffin chased with sadly ineffectual diligence by Segal and Diaz.
                    [links] => stdClass Object
                        (
                            [review] => hxxp://www.killermoviereviews.com/main.php?nextlink=display&dId=1818&subLinks=
                        )
                )
        )
)

Based on this structure you can get Andrea Chase by:

echo $data->reviews[0]->critic;

How this works is:

data is an object with a property called reviews - so $data->reviews

Now reviews is an array and we need its first index - so $data->reviews[0]

This first index is again an object with a property called critic. So based on the earlier explanation above: $data->reviews[0]->critic

The basic idea to keep in mind is that Array indexes are accessed as $array[0], $array[1] etc. Object properties are accessed as $object->property1, $object->property2 etc.

1
  • HI Raidenace , it very clear explanation , thanks you, it really works now :)
    – esi0411
    Jul 28, 2014 at 17:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.