-
-
$s='{"webname":"homehf","url":"www.homehf.com","qq":"744348666"}'; - $web=json_decode($s); //Convert characters to JSON
- $arr=array();
- foreach($web as $k=>$w) $arr[$k]=$w;< ;/p>
The first three lines can be replaced by $web=json_decode($s,true);
print_r($arr);
- ?>
- p>
-
Copy code
In the above code, a JSON object has been converted into an array, but if it is a nested JSON, the above code is obviously useless, so we write a function to solve the nested JSON ,
-
-
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 object
- else $arr[$k]=$w;
- }
- return $arr;
- }
$s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf @163.com","xx":"xxxxxxx"}}';
- $web=json_decode($s);
- $arr=json_to_array($web);
//Up One line can be replaced by $web=json_decode($s,true);
- print_r($arr);
- ?>
-
Copy code
The custom json_to_array() method can convert any Convert nested JSON to array.
|