Topics
DataTree container
The DataTree class implements a key-value tree container.
Examples
use NoreSources\DataTree;
$dataTree = new DataTree ({
'key' => 'value',
// Associative array becomes DataTree
'node' => [
'one' => 1
// Non-associative values remains as-is (array),
'two' => [ '二', 2 ],
// Associative values becomes DataTree
'three' => [
'french' => 'trois',
'spanish' => 'tres',
'italian' => 'tre'
]
]
});
// Merge content of JSON file with existing data
$dataTree->load ('content.json', DataTree::MERGE_OVERWRITE);
// Element access is available through ArrayAccess, ContainerInterface, "->" operator and the getElement() method
var_dump ($dataTree['key']);
var_dump ($dataTree['node']['two']);
if ($dataTree->has ('key')) {
$value = $dataTree->get ('key');
}
var_dump ($dataTree->node->three->italian);
var_dump ($dataTree->node->getElement('two'));
$dataTree->node->get('three')->setElement('german', 'drei');
var_dump ($dataTree->serialize()); // json