2013-02-15 36 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