Php concatenates and parses json using nested arrays to nest json
Concatenate a hybrid json-containing object array using nested Arrays
<? Php // starting from PHP 5.4 $ array = ["status" => "0", "message" => "OK", "arr" => []; class Person {public $ name; public $ age; // defines a constructor. The initialization value is public function _ construct ($ name, $ age) {$ this-> name = $ name; $ this-> age = $ age ;}}for ($ I = 0; $ I <10; $ I ++) {$ p = new Person ("ren", $ I); $ array ["arr"] [] = $ p;} // var_dump ($ array ); echo json_encode ($ array);?>
Php uses nested arrays to parse arrays of mixed json objects
<? Php function json_to_array ($ web) {$ arr = array (); foreach ($ web as $ k = >$ v) {if (is_object ($ v )) $ arr [$ k] = json_to_array ($ v); // determines whether the type is an object else $ arr [$ k] = $ v;} return $ arr ;} $ s = '{"webname": "homehf", "url": "www.homehf.com", "qq": "744348666 "}'; // convert the character to JSON $ web = json_decode ($ s); $ arr = array (); foreach ($ web as $ k => $ v) $ arr [$ k] = $ v; echo "<pre>"; print_r ($ arr); echo "</pre>"; $ s = '{"webname ": "homehf", "url": "ww W.homehf.com "," contact ": {" qq ":" 744348666 "," mail ":" nieweihf@163.com "," xx ":" xxxxxxx "}}'; $ web = json_decode ($ s); $ arr = json_to_array ($ web); echo "<pre>"; print_r ($ arr); echo "</pre> "; /*************************************** **************************************** **************************************** * **********************/$ s = '{"webname ": "homehf", "url": "www.homehf.com", "contact": {"qq": "744348666" "," Mail ":" nieweihf@163.com "," xx ":" xxxxxxx "} '; $ web = json_decode ($ s); echo 'site name :'. $ web-> webname. '<br/> URL :'. $ web-> url. '<br/> contact information: QQ -'. $ web-> contact-> qq. 'mail :'. $ web-> contact-> mail; echo '<br/> '; /*************************************** **************************************** **************************************** * **********************/$ s = '{"webname ": "homehf", "url": "www.homehf.com "," Contact ": {" qq ":" 744348666 "," mail ":" nieweihf@163.com "," xx ":" xxxxxxx "} '; $ web = json_decode ($ s ); echo json_encode ($ web); $ mys = '{"status": "0", "message": "OK", "arr": [{"name ": "ren", "age": 0 },{ "name": "ren", "age": 1 },{ "name": "ren", "age ": 2 },{ "name": "ren", "age": 3 },{ "name": "ren", "age": 4 },{ "name ": "ren", "age": 5 },{ "name": "ren", "age": 6 },{ "name": "ren", "age ": 7 },{ "name": "ren", "age": 8 },{ "name": "ren", "age": 9}]} '; $ myw Eb = json_decode ($ mys); echo $ myweb-> status; for ($ I = 0; $ I <10; $ I ++) {echo $ myweb-> arr [$ I]-> age; echo '<br/>' ;}?>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.