您的位置:首页 > 编程语言 > PHP开发

php json转换为嵌套数组array

2016-12-20 19:10 676 查看
<?php  

function json_to_array($web) {  

    $arr=array();  

    foreach($web as $k=>$v) {  

        if(is_object($v)) $arr[$k]=json_to_array($v);  //判断类型是不是object  

        else $arr[$k]=$v;  

    }  

    return $arr;  

}  

$s='{"webname":"homehf","url":"www.homehf.com","qq":"744348666"}';  

//将字符转成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":"www.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 '网站名称:'.$web->webname.'<br />网址:'.$web->url.'<br />联系方式:QQ-'.$web->contact->qq.' MAIL:'.$web->contact->mail;  

echo '<br /><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);
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: