PHP generates and obtains JSON files,

Source: Internet
Author: User

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

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.