在PHP中,我从mySQL编码了一个JSON数组,如何使用它?解码
这是我的输出:在PHP中,我从mySQL编码了一个JSON数组,如何使用它?解码,php,mysql,json,Php,Mysql,Json,这是我的输出: $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['t
$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;
如何访问阵列的单个部分,它是一个阵列阵列?我要先解码吗?试着解码
{"table data":[["1","test","30","13"],["2","test2","40","14"]]}
再见您使用的
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
其中,$id是要访问的行
比如说
$json['table data'][$id]
在向数组中添加数据时,创建数组
$json['table data'][0][1] == "test"
$json['table data'][1][1] == "test2"
你想在哪里使用它?为什么要首先对它进行编码?欢迎来到堆栈溢出!在旁注上;在新代码中。它们不再得到维护,而是在使用。看到红色的盒子了吗?而是学习准备好的语句,并使用或。
$json['table data'][] = array($row);