0

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. 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.

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.

Still fairly new to php/json stuff.

1 Answer 1

1

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). 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)

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

The echo $Qnum[$i]; should also be echo $Qnum;.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.