Following sing Xing's explanation of json usage in PHP

Source: Internet
Author: User
Following Xin Xing's explanation of json usage in PHP, because json is so important, PHP has added support for JSON since 5.2, mainly including two functions: json_encode and json_decode. As we all know, json is a data storage format. I wrote json in my blog. in my Javascript tutorial, readers who do not understand it can search for it.

For example, we create a new xin. json file and store the following data:

[{"Name": "xin xing", "age": 23 },{ "name": "Xiao Qian", "age": 20}]
You can write an api. php file to parse the json file as follows:

 The output is as follows:

Array ([0] => stdClass Object ([name] => xin xing [age] => 23) [1] => stdClass Object ([name] => Xiao Qian [age] => 20 ))
Very easy to understand. similar to Javascript, the stdClass here is the basic class. if you are not familiar with it, you can search for it and output the json data in our format, in fact, we use them almost the same as we use them in Javascript, so we will adopt a separate output style below:

  Name. "age:". $ obj [$ I]-> age ."
";}


The output result is as follows:

Name: Xin Xing Age: 23 name: Xiao Qian Age: 20
The json_decode function can accept the second parameter. if it is true, our json data will be converted to an array instead of an object, in fact, objects and arrays are often very similar, and in PHP, objects do not call methods.
Now that our team's json_decode is clear, the json_encode is easy to use by the readers ....
Related Article

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.