牛骨文教育服务平台(让学习变的简单)
博文笔记

php中输出json对象的值

创建时间:2017-04-20 投稿人: 浏览次数:4767
 <!DOCTYPE html>
<html>
<body>

<?php
 
$json = "{"report":{"date":"2012-04-10","content":"abcdefght"}}";
  $arr = (array) json_decode($json,true);
  echo "当前日期是:". $arr["report"]["date"];
echo "<br/>";

echo "<pre>";
  print_r($arr);
echo "</pre>";  
  
  foreach($arr as $arrvalue)
  {
    foreach($arrvalue as $key=>$value)
    {
      echo "[$key] = $value <br />";
    }
  }
echo "<br/>";
?>

 
</body>
</html>


<!DOCTYPE html>
<html>
<body>

<?php
$file_exists = file_exists("json.txt");
if( $file_exists ){
    $myfile = fopen("json.txt", "r") or die("");
    $content = fread($myfile,filesize("json.txt"));
    fclose($myfile);
    echo $content;
    echo "<br/>";
    $arr=(array)json_decode($content,true);
    echo $arr["CSS"];
    echo "<br/>";
    echo $arr["AJAX"];
}


?>
  
</body>
</html>



json.txt:

{
    "AJAX": "Asynchronous JavaScript and XML",
    "CSS": "Cascading Style Sheets"
}

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。