The example in this article describes how PHP generates and obtains JSON files. Share to everyone for your reference, specific as follows:
First defines an array, then traverses the data table, puts the corresponding data into the array, and finally transforms the array by Json_encode ()
The function of the Json_encode () function is to convert a numeric value into a JSON data storage format.
putjson.php:
<?php
//Generate a PHP array
$data = Array ();
$data [0] = array (' 1 ', ' Wu-ran ', ' onestopweb.cn ');
$data [1] = Array (' 2 ', ' How to open ', ' iteye.com ');
Turn the PHP array into a JSON string
$json _string = Json_encode ($data);
Write file
file_put_contents (' Test.json ', $json _string);
? >
A JSON file with the same name is overwritten and not created.
The JSON that is generated or overwritten is as follows:
Copy Code code as follows:
[["1", "\u811a\u672c\u4e4b\u5bb6", "Www.jb51.net"],["2", "\u7f16\u7a0b\u5f00\u53d1", "jb51.net"]]
The data in the JSON file is then read into the PHP variable.
getjson.php:
<?php
//Read data from file to PHP variable
$json _string = file_get_contents (' Test.json ');
Turn the JSON string into a php array
$data = Json_decode ($json _string, true);
Show it to see
var_dump ($data);
Echo ' <br><br> ';
Print_r ($data);
Echo ' <br><br> ';
Echo ' number: '. $data [0][0]. ' Name: '. $data [0][1]. ' URL: '. $data [0][2];
Echo ' <br> ';
Echo ' number: '. $data [1][0]. ' Name: '. $data [1][1]. ' URL: '. $data [1][2];
? >
Effect Chart:
PS: Here again for you to recommend a few more practical JSON online tools for your reference to use:
Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json
JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat
C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json
For more information about PHP interested readers can view the site topics: "PHP JSON format data Operation tips Summary", "PHP for XML file Operation skills Summary", "PHP basic Grammar Introductory Course", "PHP Array" Operation Techniques Encyclopedia, "PHP string ( String) Usage summary, "Getting Started with Php+mysql database operations" and "Summary of common PHP database Operations Tips"
I hope this article will help you with the PHP program design.