This article mainly introduces PHP generation and get JSON file method, involving PHP for JSON format data and file-related operations skills, the need for friends can refer to the following
The example in this paper describes how PHP is generated and gets JSON files. Share to everyone for your reference, as follows:
First define an array, then traverse the data table, put the corresponding data into the array, and finally transform 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//generates a PHP array $data = array (); $data [0] = array (' 1 ', ' ng ', ' onestopweb.cn '); $data [1] = Array (' 2 ', ' No ', ' iteye.com ' );//convert PHP array to 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 is not created.
The JSON generated or overwritten is as follows:
Copy the Code code as follows:
[["1", "\u811a\u672c\u4e4b\u5bb6", "www.php.cn"],["2", "\u7f16\u7a0b\u5f00\u53d1", "php.cn"]]
The data in the JSON file is then read into the PHP variable.
getjson.php:
<?php//read data from a file to php variable $json_string = file_get_contents (' Test.json ');//Convert JSON string to php array $data = Json_decode ($json _ string, True);//show it to see Var_dump ($data), echo ' <br><br> ';p rint_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] . ' Website: '. $data [note];? >
: