简体   繁体   English

在PHP上遍历Json数组

[英]Looping through Json array on PHP

What I have is an array that has several values 我所拥有的是一个具有多个值的数组

Array
(
    [result] => 1
    [message] => Query Successful
    [data] => Query Output
    [0] => Array
    (
        [QNO] => 1
        [SNAME] => test1
        [QDESC] => testing
    )

    [1] => Array
    (
        [QNO] => 2
        [SNAME] => test2
        [QDESC] =>  testing

    )

and so on with more data. 等等获得更多数据 I want to be able to extract that in my php script. 我希望能够将其提取到我的PHP脚本中。 The data shown was from printing the decoded array with 显示的数据来自使用以下命令打印解码后的数组

$data = json_decode($json, true);

I can print everything with print_r() but I want to be able to print using a loop so I can create a table with it. 我可以使用print_r()打印所有内容,但是我希望能够使用循环进行打印,因此可以使用它创建表。

To try an output of all the question numbers I tried a rough for loop using 为了尝试所有问题编号的输出,我尝试使用

for($i = 0; $i <=5; i++){
 $Qnum = $data[$i]['QNO'];
 echo $Qnum[$i];
 $i++;

}

but this seems to just keep loading and loading the script with no output. 但这似乎只是继续加载和加载脚本而没有输出。 The print_r($data) prints everything out as shown above. 如上所示, print_r($data)所有内容打印出来。

Still fairly new to php/json stuff. 仍然是php / json的新东西。

It's a syntax error. 这是语法错误。

You need to add the $ before i++, and remove the $i++ inside the loop (or you'll increment $i twice). 您需要在i ++之前添加$,然后在循环内删除$ i ++(否则您将使$ i递增两次)。 You don't see anything on screen because probably the server is configured not to display any error. 屏幕上看不到任何内容,因为可能服务器已配置为不显示任何错误。 At the beginning of the script add ini_set('display_errors', 1) 在脚本的开头添加ini_set('display_errors', 1)

for($i = 0; $i <=5; $i++){
 $Qnum = $data[$i]['QNO'];
 echo $Qnum;

}

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

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