How PHP generates and gets JSON files

Source: Internet
Author: User
Tags php array to json
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];? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.