I have a class:
class NodoArbolDHTML{
var $id;
var $name;
var $data;
var $children;
public function __construct($id,$name,$resultado,$tipo){
$this->id = $id;
$this->name = $name;
$this->data = new datos($resultado,$tipo);
}
public function anadirHijo($nodoHijo, $id){
//anadir un hijo
if (!isset($this->children)){
$this->children = array();
}
$this->children[$id] = $nodoHijo;
$this->children = array_values($this->children);
}
}
With which I can create an object of this type:
$root= NodoArbolDHTML Object (
[id] => 38358
[name] => Anillo Critico
[data] => datos Object ( [color] => #008000 [type] => circle )
[children] => Array (
[0] => NodoArbolDHTML Object (
[id] => 38347
[name] => Transito
[data] => datos Object ( [color] => #008000 [type] => circle )
[children] =>
)
[1] => NodoArbolDHTML Object (
[id] => 43052
[name] => Centros de Acceso
[data] => datos Object ( [color] => #008000 [type] => circle )
[children] =>
)
)
)
And I can access an object of the array:
print_r($root->children[0]); :
NodoArbolDHTML Object (
[id] => 38347
[name] => Transito
[data] => datos Object (
[color] => #008000
[type] => circle
)
[children] =>
)
Then I do a $arbol=json_encode($root)
and get an object in JSON notation:
// $arbol
{
"id": "38358",
"name": "Anillo Critico",
"data": {
"$color": "#008000",
"$type": "circle"
},
"children": [
{
"id": "38347",
"name": "Transito",
"data": {
"$color": "#008000",
"$type": "circle"
},
"children": null
},
{
"id": "43052",
"name": "Centros de Acceso",
"data": {
"$color": "#008000",
"$type": "circle"
},
"children": null
}
]
}
I need a function inside the class NodoArbolDHTML
that does the reverse process, to which I give the JSON in JSON notation.
In this way, you could:
- use the add functionHijo
-
Go through the array, looking for the object that interests me:
print_r($root->children[0]); NodoArbolDHTML Object ( [id] => 38347 [name] => Transito [data] => datos Object ( [color] => #008000 [type] => circle ) [children] => )