$classconducted = json_encode(array('segment'=>$segment,'Board'=>$board,'classFiveSubject'=>$subject5,'classeightboard'=>$eightboard,'classeightsubject'=>$subject8,'classTenthboard'=>$tenthboard,'classTenthsubject'=>$subject8,'engineering'=>$engineering));
$in = '{"segment":["I-V 类学费","VI-VIII 类学费","IX-X 类学费"],"董事会":["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] =>
)
I have used the following code to retrieve the data. but I could not workout it.can any one guide me how to solve this?
<?php
$sql=mysql_query("select * from tinfo");
while($row=mysql_fetch_array($sql))
{
$in=$row['classconducted'];
}
echo "<pre>";
$value=json_decode($in, true);//echo count($in); exit;
foreach ($value as $k => $val)
{
echo "$k | $val <br />";
}
?>
输出是:这里它列出了索引键,并且值没有出现如何在这里使用foreach。我想检索键和值。
segment | Array
Board | Array
classFiveSubject | Array
classeightboard | Array
classeightsubject | Array
classTenthboard |
classTenthsubject | Array
engineering | Array
I want the output form my json_decode like this:
segment:Class I-V Tuition
Board:CBSE,STATE
classFiveSubject:AllSubject,Maths,Science
segment:Class VI-VIII Tuition
Board:CBSE, STATE
classEightSUbject:AllSubject,Maths,Science
谁能给我正确的代码? 列出json_decode数据的方法是什么。
未经测试,但请尝试:
foreach (json_decode($in, true) as $key => $val) {
echo $key.': ';
if (is_array($val))
echo implode(',', $val) . "'n";
else
echo $val . "'n";
}
define('BR', '<br />');
$json = json_decode($in, true);
foreach ($json as $key => $arr ) {
echo $key.BR;
foreach( $arr as $value ) {
echo $value.BR;
}
}