我试图在PHP中以数组的形式获取JSON文件中的信息,所以我可以将它放入我的数据库中。在这个简单的例子我的代码工作:在PHP中获取JSON信息
{
"tiles":[179, 199, 198, 198, 160, 198, 180, 199, 178, 160, 180, 180, 198, 160]
}
$string = file_get_contents(FileDestination);
$worldarray = json_decode($string, true);
foreach($worldarray['tiles'] as $item) {
mysqli_query($link, 'INSERT INTO blabla (test) VALUES('.$item.')');
}
然而,恰巧我的JSON文件是一个比较复杂:
{ "height":32,
"layers":[
{
"data":[179, 199, 198, 198, 160, 198, 180, 199, 178, 160, 180, 180, 198, 160, 178,],
"height":32,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":32,
"x":0,
"y":0
},
等。我需要图层>数据中的信息。我尝试过以各种方式访问它:$ worldarray ['layers'] ['data']但我似乎无法从数据中获取值。谁能帮我这个?提前致谢。
它只看起来更复杂,但事实并非如此。只是嵌套。 'foreach($ worldarray ['layers'] [0] ['data'] as $ item)' – DanFromGermany
首先; 'print_r(json_decode($ string,true))的输出是什么? (ot:nice nick,jan lul;)) – giorgio
请显示json数据,因为它在文件中... – Lab