Analysis of php json operation instances
This article mainly introduces common PHP methods for JSON operations. The example analyzes json-to-array conversion, array-to-json conversion, and other skills and related precautions. For more information, see
This article analyzes php json operations. Share it with you for your reference. The specific analysis is as follows:
JSON can be used in many programming languages, so we can use it for small data transfer, for example, PHP outputs JSON strings for JavaScript. In PHP, you can use json_decode () to parse JSON objects from a string of standard strings, and use json_encode () to generate a string of standard strings from JSON objects.
Example:
The Code is as follows:
<? Php
$ Json = '{"a": 1, "B": 2, "c": 3, "d": 4, "e": 5 }';
Var_dump (json_decode ($ json ));
Var_dump (json_decode ($ json, true ));
Output:
The Code is as follows:
Object (stdClass) #1 (5 ){
["A"] => int (1)
["B"] => int (2)
["C"] => int (3)
["D"] => int (4)
["E"] => int (5)
}
Array (5 ){
["A"] => int (1)
["B"] => int (2)
["C"] => int (3)
["D"] => int (4)
["E"] => int (5)
}
The Code is as follows:
$ Arr = array ('A' => 1, 'B' => 2, 'c' => 3, 'D' => 4, 'E' => 5 );
Echo json_encode ($ arr );
Output: {"a": 1, "B": 2, "c": 3, "d": 4, "e": 5}
1. json_decode (), which converts characters to JSON and is generally used to receive data sent by Javascript.
The Code is as follows:
<? Php
$ S = '{"webname": "homehf", "url": "www.homehf.com", "contact": {"qq": "123456789", "mail ": "nieweihf@163.com", "xx": "xxxxxxx "}}';
$ Web = json_decode ($ s );
Echo 'website name :'. $ web-> webname. '<br/> URL :'. $ web-> url. '<br/> contact information: QQ -'. $ web-> contact-> qq. 'mail :'. $ web-> contact-> mail;
?>
In the above example, we first define a variable s, and then parse it into a JSON object using json_decode (), which can be used in JSON mode. From the usage perspective, similar to JSON, XML, and array functions, JSON can store data that is related to each other. However, I personally think JSON is easier to use and JSON and JavaScript can be used for data sharing.
2. json_encode (), JSON to character. This is generally used in AJAX applications to convert a JSON object into a string and output it to Javascript, but it is also used in database storage.
The Code is as follows:
<? Php
$ S = '{"webname": "homehf", "url": "www.homehf.com", "contact": {"qq": "123456789", "mail ": "nieweihf@163.com", "xx": "xxxxxxx "}}';
$ Web = json_decode ($ s );
Echo json_encode ($ web );
?>
Ii. Convert php json to array
The Code is as follows:
<? Php
$ S = '{"webname": "homehf", "url": "www.homehf.com", "qq": "123456789 "}';
$ Web = json_decode ($ s); // convert the character to JSON
$ Arr = array ();
Foreach ($ web as $ k =>$ w) $ arr [$ k] = $ w;
Print_r ($ arr );
?>
In the code above, a JSON object has been converted into an array, but if it is nested JSON, the code above is obviously powerless, then we write a function to solve nested JSON,
The Code is as follows:
<? Php
$ S = '{"webname": "homehf", "url": "www.homehf.com", "contact": {"qq": "123456789", "mail ": "nieweihf@163.com", "xx": "xxxxxxx "}}';
$ Web = json_decode ($ s );
$ Arr = json_to_array ($ web );
Print_r ($ arr );
Function json_to_array ($ web ){
$ Arr = array ();
Foreach ($ web as $ k => $ w ){
If (is_object ($ w) $ arr [$ k] = json_to_array ($ w); // judge whether the type is an object.
Else $ arr [$ k] = $ w;
}
Return $ arr;
}
?>
I hope this article will help you with php programming.