note 103137 modified in function.json-encode by danbrown
| From: | danbrown@php.net | Date: | Mon, 28 Mar 2011 16:50:04 +0000 |
| Subject: | note 103137 modified in function.json-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178267@lists.php.net to get a copy of this message | ||
As json_encode() is recursive, you can use it to serialize whole structure of objects.
<?php
class A {
public $a = 1;
public $b = 2;
public $collection = array();
function __construct(){
for ( $i=3; $i-->0;){
array_push($this->collection, new B);
}
}
}
class B {
public $a = 1;
public $b = 2;
}
echo json_encode(new A);
?>
Will give:
{
"a":1,
"b":2,
"collection":[{
"a":1,
"b":2
},{
"a":1,
"b":2
},{
"a":1,
"b":2
}]
}
--was--
As json_encode() is recursive, you can use it to serialize whole structure of objects.
class A {
public $a = 1;
public $b = 2;
public $collection = array();
function __construct(){
for ( $i=3; $i-->0;){
array_push($this->collection, new B);
}
}
}
class B {
public $a = 1;
public $b = 2;
}
echo json_encode(new A);
Will give:
{
"a":1,
"b":2,
"collection":[{
"a":1,
"b":2
},{
"a":1,
"b":2
},{
"a":1,
"b":2
}]
}
http://php.net/manual/en/function.json-encode.php