Home  >  Article  >  Backend Development  >  How to output json object in php

How to output json object in php

php中世界最好的语言
php中世界最好的语言Original
2018-03-24 16:12:344171browse

This time I will show you how to output json objects in PHP. What are the precautions for outputting JSON objects in PHP? The following is a practical case, let's take a look.

An example is as follows:

 <!DOCTYPE html>
<html>
<body>
<?php
 
$json = &#39;{"report":{"date":"2012-04-10","content":"abcdefght"}}&#39;;
 $arr = (array) json_decode($json,true);
 echo &#39;当前日期是:&#39;. $arr[&#39;report&#39;][&#39;date&#39;];
echo "<br/>";
echo '<pre class="brush:php;toolbar:false">';
 print_r($arr);
echo '
';     foreach($arr as $arrvalue)  {  foreach($arrvalue as $key=>$value)  {   echo "[$key] = $value 
";  }  } echo "
"; ?>  

<!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"
}
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to operate format files in php

##Build a PHP development environment in Docker

The above is the detailed content of How to output json object in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn