user2715480
user2715480

Reputation: 33

How to retrieve the json_encode values to user side

My table name is tinfo and columnname is classconducted. column conducted class contains like this.

{"segment":["Class I-V Tuition","Class VI-VIII Tuition","Class IX-X Tuition"],"Board":["cbse","cse/Ise","State"],"classFiveSubject":["allsubject","science"],"classeightboard":["cbse","cse/Ise"],"classeightsubject":null,"classTenthboard":["cbse","cse/Ise"],"classTenthsubject":null,"engineering":null}

I want output should look like this:

classconducted.
segment:Class I-V Tuition
Board:CBSE,STATE
classFiveSubject:AllSubject,Maths,Science

segment:Class VI-VIII Tuition
Board:CBSE, STATE
classEightSUbject:AllSubject,Maths,Science .

And my index having engineering is null here I don't want  to show the null values, can any one guide me. what I have write in query.
<?php
$sql=mysql_query("select * from tinfo");
while($row=mysql_fetch_array($sql))
{
echo $row['classconducted'];
}
?>

Upvotes: 1

Views: 34

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94672

There is a PHP function called json_decode() see manual

Basically if you run the json string through this function it will convert it to a PHP variable i.e. if the json string is an object you get a PHP object and if its a json array you get a PHP array ... etc

So take your json string and pass it through json_decode() and you will get

$in = '{"segment":["Class I-V Tuition","Class VI-VIII Tuition","Class IX-X Tuition"],"Board":["cbse","cse/Ise","State"],"classFiveSubject":["allsubject","science"],"classeightboard":["cbse","cse/Ise"],"classeightsubject":null,"classTenthboard":["cbse","cse/Ise"],"classTenthsubject":null,"engineering":null}';

print_r( json_decode($in) );

Output =

stdClass Object
(
    [segment] => Array
        (
            [0] => Class I-V Tuition
            [1] => Class VI-VIII Tuition
            [2] => Class IX-X Tuition
        )

    [Board] => Array
        (
            [0] => cbse
            [1] => cse/Ise
            [2] => State
        )

    [classFiveSubject] => Array
        (
            [0] => allsubject
            [1] => science
        )

    [classeightboard] => Array
        (
            [0] => cbse
            [1] => cse/Ise
        )

    [classeightsubject] =>
    [classTenthboard] => Array
        (
            [0] => cbse
            [1] => cse/Ise
        )

    [classTenthsubject] =>
    [engineering] =>
)

Now you should be able to process it using standard PHP code.

Upvotes: 1

Related Questions