简体   繁体   English

在PHP中,我从mySQL编码了一个JSON数组,如何使用它? 解码?

[英]In PHP, I encoded a JSON array from mySQL, how do I use it? decode?

$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; 

This is my output: 这是我的输出:

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

How do I access an individual piece of the array, its an array of arrays? 如何访问数组的单个部分,即数组的数组? do i decode it first? 我先解码吗?

Try to decode 尝试解码

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

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

Bye Bye 再见

you use 你用

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

where $id is the row you want to access $ id是您要访问的行

so for example 例如

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

将数据添加到数组时,创建一个数组。

$json['table data'][] = array($row);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM