如何将json_encode值检索到用户端

时间:2015-02-18 11:43:26

标签: php

我的表名是tinfo,列名是classconducted。列进行类包含这样的。

{"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}

我希望输出看起来像这样:

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'];
}
?>

1 个答案:

答案 0 :(得分:1)

有一个名为json_decode() see manual

的PHP函数

基本上,如果你通过这个函数运行json字符串,它会将它转换为PHP变量,即如果json字符串是一个对象,你得到一个PHP对象,如果它是一个json数组,你得到一个PHP数组......等等/ p>

所以拿你的json字符串并通过json_decode()传递它,你就会得到

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

输出=

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] =>
)

现在您应该能够使用标准PHP代码处理它。