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 ....