php json转换为嵌套数组array


[php]
  view plain  copy
  1. <?php  
  2. function json_to_array($web) {  
  3.     $arr=array();  
  4.     foreach($web as $k=>$v) {  
  5.         if(is_object($v)) $arr[$k]=json_to_array($v);  //判断类型是不是object  
  6.         else $arr[$k]=$v;  
  7.     }  
  8.     return $arr;  
  9. }  
  10. $s='{"webname":"homehf","url":"www.homehf.com","qq":"744348666"}';  
  11. //将字符转成JSON  
  12. $web=json_decode($s);  
  13. $arr=array();  
  14. foreach($web as $k=>$v)  
  15.     $arr[$k]=$v;  
  16. echo "<pre>";  
  17. print_r($arr);  
  18. echo "</pre>";  
  19.   
  20. $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}';  
  21. $web=json_decode($s);  
  22. $arr=json_to_array($web);  
  23. echo "<pre>";  
  24. print_r($arr);  
  25. echo "</pre>";  
  26.   
  27. /************************************************************************ 
  28. ************************************************************************/  
  29. $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}';  
  30. $web=json_decode($s);  
  31. echo '网站名称:'.$web->webname.'<br />网址:'.$web->url.'<br />联系方式:QQ-'.$web->contact->qq.' MAIL:'.$web->contact->mail;  
  32. echo '<br /><br />';  
  33. /************************************************************************ 
  34. ************************************************************************/  
  35. $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}';  
  36. $web=json_decode($s);  
  37. echo json_encode($web);  
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值