我試圖在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