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);
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);
相关文章推荐
- php json转换为嵌套数组array
- php json array convert 嵌套 转换 操作
- Php的数组转换成XML(php Array to Xml)
- php数组转换为对象PHP - Convert Array to Object with stdClass
- PHP数组和Json之间的转换
- php 利用json_decode强制json数据转换成数组
- php json与数组转换及中文乱码问题
- [php] 如何将 simplexml_load_string 转换成数组array
- PHP之-json转数组,支持多层嵌套json
- PHP数组和Json之间的转换
- PHP XML To Array,将XML转换为数组
- PHP数组和Json之间的转换
- Php json_encode转换数组,值为null
- 用json传过来的数组并不是标准的array,所以需要用这个函数进行转换。
- PHP数组和Json之间的转换
- 在CakePHP中利用Javascript Helper把PHP数组转换为JSON
- PHP数组和Json之间的转换
- PHP中数组转换成json字符串程序代码
- PHP数组和Json之间的转换