【问题标题】:I want to retrieve the json_decode data. How to write foreach here我想检索 json_decode 数据。如何在这里写 foreach
【发布时间】:2016-12-26 19:57:04
【问题描述】:
$classconducted = json_encode(array('segment'=>$segment,'Board'=>$board,'classFiveSubject'=>$subject5,'classeightboard'=>$eightboard,'classeightsubject'=>$subject8,'classTenthboard'=>$tenthboard,'classTenthsubject'=>$subject8,'engineering'=>$engineering));

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

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数据的方法有哪些。

【问题讨论】:

  • $arr = json_decode($in, TRUE); foreach ( $arr as $key => $data ){ /* $data 是一个数组? */ }
  • 我已经完成了。之后它显示这样。你能触发吗?

标签: php


【解决方案1】:

未经测试,但尝试:

foreach (json_decode($in, true) as $key => $val) {

  echo $key.': ';
  if (is_array($val))
    echo implode(',', $val) . "\n";
  else
    echo $val . "\n";
  
}

【讨论】:

  • 太棒了。我真诚地感谢你。我会做的很简单。 $classconducted = json_encode(array('segment'=>$segment,'Board'=>$board,'classFiveSubject'=>$subject5,'classeightboard'=>$eightboard,'classeightsubject'=>$subject8,'classTenthboard' =>$tenthboard,'classTenthsubject'=>$subject8,'engineering'=>''));工程索引不包含任何值如何避免空值它显示没有值的空键那些不包含任何值。我也更新了问题
【解决方案2】:
define('BR', '<br />');

$json = json_decode($in, true);

foreach ($json as $key => $arr ) {
    echo $key.BR;

    foreach( $arr as $value ) {
        echo $value.BR; 
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多