php利用嵌套数组拼接与解析json的方法 利用嵌套数组 拼接混合json -包含对象数组 "0", "message" => "ok", "arr"=> [] ]; class Person { public $name; public $age; //定义一个构造方法初始化赋值 public function __construct($name,$age) { $this->name=$name; $this->age=$age; } } for($i=0;$i<10;$i++) { $p=new Person("ren",$i); $array["arr"][]=$p; } //var_dump($array); echo json_encode($array); ?> php利用嵌套数组 解析混合json 包含对象数组 $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 "
"; print_r($arr); echo ""; $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 "
"; print_r($arr); echo ""; /************************************************************************ ************************************************************************/ $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}'; $web=json_decode($s); echo '网站名称:'.$web->webname.'