php利用嵌套数组拼接与解析json的方法

利用嵌套数组 拼接混合json -包含对象数组

rush:PHP;"> // 自 PHP 5.4 起
$array = [
"status" => "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 包含对象数组

rush:PHP;"> $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","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","xx":"xxxxxxx"}}';
$web=json_decode($s);
echo '网站名称:'.$web->webname.'
网址:'.$web->url.'
联系方式:QQ-'.$web->contact->qq.' MAIL:'.$web->contact->mail;
echo '

';
/****
****/
$s='{"webname":"homehf","xx":"xxxxxxx"}}';
$web=json_decode($s);
echo json_encode($web);

$mys='{"status":"0","message":"ok","arr":[{"name":"ren","age":0},{"name":"ren","age":1},"age":2},"age":3},"age":4},"age":5},"age":6},"age":7},"age":8},"age":9}]}';

$myweb=json_decode($mys);

echo $myweb->status;

for($i=0;$i<10;$i++)
{
echo $myweb->arr[$i]->age;
echo '

';
}
?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...