PHP generates and obtains JSON files,
This example describes how to generate and obtain JSON files in PHP. We will share this with you for your reference. The details are as follows:
First define an array, then traverse the data table, put the corresponding data into the array, and finally convert the array through json_encode ()
The json_encode () function converts a value to a JSON data storage format.
Putjson. php:
<? Php // generate a PHP array $ data = array (); $ data [0] = array ('1', 'wu Zhe ran ', 'onestopweb. cn '); $ data [1] = array ('2', '', 'iteye. com '); // convert the PHP array to a JSON string $ json_string = json_encode ($ data); // write the file file_put_contents ('test. json ', $ json_string);?>
If a JSON file with the same name exists, it is overwritten. If no, it is created.
The generated or overwritten JSON is as follows:
Copy codeThe Code is as follows: [["1", "\ u811A \ u672C \ u4E4B \ u5BB6", "www.jb51.net"], ["2 ", "\ u7F16 \ u7A0B \ u5F00 \ u53D1", "jb51.net"]
Then, read the data in the JSON file to the PHP variable.
Getjson. php:
<? Php // read data from the file to the PHP variable $ json_string = file_get_contents ('test. json'); // convert the json string into a PHP array $ data = json_decode ($ json_string, true); // display it to see var_dump ($ data ); echo '<br>'; print_r ($ data); echo '<br>'; echo 'No :'. $ data [0] [0]. 'name :'. $ data [0] [1]. 'url :'. $ data [0] [2]; echo '<br>'; echo 'No :'. $ data [1] [0]. 'name :'. $ data [1] [1]. 'url :'. $ data [1] [2];?>
:
PS: Here are some useful json online tools for your reference:
Online JSON code verification, validation, beautification, and formatting tools:
Http://tools.jb51.net/code/json
JSON online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson
Json code online formatting/beautification/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat
C language style/HTML/CSS/json code formatting and beautification tools:
Http://tools.jb51.net/code/ccode_html_css_json