$w) $arr[$k]=$w;前三行可以用$web=json_decode($s,true)代替;
print_r($arr);
?>
复制代码
上面代码中,已经将一个json对象转成了一个数组,可是如果是嵌套的json,上面的代码显然无能为力了,那么我们写一个函数解决嵌套json,
$w){ if(is_object($w)) $arr[$k]=json_to_array($w); //判断类型是不是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);//上一行可以用$web=json_decode($s,true)代替;
print_r($arr);?>
复制代码
自定义的json_to_array()方法可以将任何嵌套的json转成数组。