2013-04-11 18 views
0
$sql = "SELECT * FROM `scripts` LIMIT 0, 30 "; 
$result = mysql_query($sql) or die ('Error updating database: ' . mysql_error()); 
$json = array(); 
if(mysql_num_rows($result)) { 
    while($row=mysql_fetch_row($result)) { 
     $json['table data'][]=$row; 
    } 
} 
$encoded = json_encode($json); 
echo $encoded; 

這是我的輸出:在PHP中,我從mySQL編碼了一個JSON數組,我該如何使用它?解碼?

{"table data":[["1","test","30","13"],["2","test2","40","14"]]} 

如何訪問數組,其數組的數組的單個物品?我是否先解碼它?

+3

你想在哪裏使用它? – itachi 2013-04-11 21:46:11

+0

爲什麼你首先編碼它? – showdev 2013-04-11 21:47:59

+0

歡迎來到Stack Overflow!在附註中; [請不要在新代碼中使用'mysql_ *'函數](http://stackoverflow.com/q/12859942/1190388)。他們不再被維護,並[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到紅色框?改爲了解準備好的語句,然後使用[tag:PDO]或[tag:MySQLi]。 – hjpotter92 2013-04-11 21:48:19

回答

0

您使用

$json['table data'][$id] 

其中的$ id是您要訪問

因此,例如行

$json['table data'][0][1] == "test" 
$json['table data'][1][1] == "test2" 
+0

謝謝!這工作 – 2013-04-11 21:59:52

1

嘗試解碼

$json = '{"foo-bar": 12345}'; 

$obj = json_decode($json); 
print $obj->{'foo-bar'}; // 12345 

再見

+0

+1 * Bye Bye * – hjpotter92 2013-04-11 21:49:21

0

將數據添加到數組中時創建一個數組。

$json['table data'][] = array($row); 
相關問題