使用php解析json单个数组而没有foreach [重复]

Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code

PHP code to parse json:

<?php
$url = "http://xxx.com/request-url?type=json";
$string = file_get_contents($url);      
$json = json_decode($string);

if (count($json)) {
  echo $json->book->title;
} else {
  echo "No book found";
}
?>

Example json response:

{"book":[{"title":"Good day"}]}

Because of single array, i've removed foreach: foreach($json->book as $books)

I tried:
$json->book->title,
$json->title,
$json[book][title],
$json[title].

All not working.

Any help?

When you do print_r($json) you get

stdClass Object
(
    [book] => Array
        (
            [0] => stdClass Object
                (
                    [title] => Good day
                )

        )

)

You access StdClass properties with -> and arrays with []

So, obviously, it's

$json->book[0]->title;

Click for Demo

If this is not working, then your JSON is likely invalid. Check it with http://www.jsonlint.com