How to output json object in php

php中世界最好的语言
Release: 2023-03-22 08:50:02
Original
4170 people have browsed it

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 "
"; ?>  
Copy after login

<!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>
Copy after login

json.txt:

{
 "AJAX": "Asynchronous JavaScript and XML",
 "CSS": "Cascading Style Sheets"
}
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!