2013-02-15 53 views
-1

我無法找到如何在此JSON數據中回顯「標籤」。JSON PHP數組解碼 - 訪問特定數據

{"totalHits":26,"hits":[{"previewHeight":92,"tags":"sunflower, sunflower field, flora"}]}; 

我可以回聲 「totalHits」,用這樣的:

$json = file_get_contents($url); 
$obj = json_decode($json); 
echo $obj->totalHits; // 26 
+4

首次使用var_dump($ obj) – 2013-02-15 21:32:22

回答

0

我會強烈建議使用print_r,使你更容易追查陣列

print_r($obj);

stdClass Object 
(
    [totalHits] => 26 
    [hits] => Array 
     (
      [0] => stdClass Object 
       (
        [previewHeight] => 92 
        [tags] => sunflower, sunflower field, flora 
       ) 

     ) 

) 

輸出所以你的對象可以像這樣訪問

echo $obj->hits[0]->tags; 
3

以可讀格式在你的JSON尋找

{ 
    "totalHits": 26, 
    "hits": [{ 
     "previewHeight": 92, 
     "tags": "sunflower, sunflower field, flora" 
    }] 
}; 

我們可以看到,tags是的屬性hit對象

$obj->hits是一個包含hit個對象

所以......

echo $obj->hits[0]->tags; 
+0

謝謝!這樣可行! – hdeh 2013-02-15 21:35:22