This article mainly introduces the method of splicing and parsing json using nested arrays in PHP. The content is quite good. I will share it with you now and give it as a reference.
Use nested arrays to splice mixed json-containing object arrays
<?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 usage Nested array parsing mixed json containing object array
<?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 class="brush:php;toolbar:false">"; 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.'
The above is the entire content of this article, I hope it will be helpful to everyone's learning, For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
PHP
Parameter description of CURL CURLOPT
How to implement PHP grouping according to the value of the array
The above is the detailed content of Use php nested arrays to splice and parse json. For more information, please follow other related articles on the PHP Chinese website!