2013-06-25 70 views
5

我如何从JSON对象中的数据,因为这与PHP?:如何在PHP中获取JSON数据?

[ 
{ 
    "name": "Anders", 
}, 
{ 
    "name": "Chris", 
}, 
{ 
    "name": "Peter", 
} 
] 

这是PHP代码,我有:

$url = 'http://urltotheapi'; 
$content = file_get_contents($url); 
$json = json_decode($content, true); 

回答

1
foreach($json as $i){ 
    echo $i['name']; 
} 
6

同其他任何PHP结构。

echo $json[1]['name']; 
+0

哦,我忘了,我想要显示每个名字。 – anderskristo

+0

那又如何?这就是'foreach()'的用处。 –

1

注意json_decode转换JSON字符串到一个数组 - 并通过指定作为真正的第二个参数,这将是一个关联数组。因此,您可以像访问Ignacio和Jleagle指定的普通数组一样访问包含在其中的数据。您可以迭代数据或逐个显示它。