最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php利用嵌套数组拼接与解析json的方法
时间:2022-06-24 22:42:38 编辑:袖梨 来源:一聚教程网
利用嵌套数组 拼接混合json -包含对象数组
代码如下 | 复制代码 |
// 自 PHP 5.4 起 $array= [ "status"=>"0", "message"=>"ok", "arr"=> [] ];
classPerson { public$name; public$age;
//定义一个构造方法初始化赋值 publicfunction__construct($name,$age) { $this->name=$name; $this->age=$age; } }
for($i=0;$i<10;$i++) { $p=newPerson("ren",$i); $array["arr"][]=$p; }
//var_dump($array);
echojson_encode($array);
?> |
php利用嵌套数组 解析混合json 包含对象数组
代码如下 | 复制代码 |
functionjson_to_array($web) { $arr=array(); foreach($webas$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($webas$k=>$v) $arr[$k]=$v; echo" "; print_r($arr); echo"";
$s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"[email protected]","xx":"xxxxxxx"}}' $web=json_decode($s); $arr=json_to_array($web); echo" "; print_r($arr); echo"";
/************************************************************************ ************************************************************************/ $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"[email protected]","xx":"xxxxxxx"}}' $web=json_decode($s); echo'网站名称:'.$web->webname.' echo' /************************************************************************ ************************************************************************/ $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"[email protected]","xx":"xxxxxxx"}}' $web=json_decode($s); echojson_encode($web);
$mys='{"status":"0","message":"ok","arr":[{"name":"ren","age":0},{"name":"ren","age":1},{"name":"ren","age":2}, {"name":"ren","age":3},{"name":"ren","age":4},{"name":"ren","age":5},{"name":"ren","age":6},{"name":"ren","age":7}, {"name":"ren","age":8},{"name":"ren","age":9}]}'
$myweb=json_decode($mys);
echo$myweb->status;
for($i=0;$i<10;$i++) { echo$myweb->arr[$i]->age; echo' } ?> |
相关文章
- 以闪亮之名店长体验流霞季怎么玩 缘溪临霞套装活动介绍 12-31
- 未定事件簿旧梦新生左然篇怎么玩 旧梦新生左然篇活动介绍 12-31
- 未定事件簿左然破浪远行怎么样 12-31
- 桃源深处有人家行医问诊怎么玩 12-31
- 恋与制作人跨年福利有哪些 恋与制作人跨年福利内容介绍 12-31
- 阴阳师协同对弈大乱斗怎么玩 阴阳师协同对弈大乱斗活动介绍 12-31