For the purpose of my website, I need to dynamically produce object keys for my JSON tree structure, at runtime. My structure is nested, with each parent node containing an array of child nodes. This nested structure makes things quite complicated, and can make the key very long as the tree gets deeper. I cannot change this however, as it is needed for the tree drawing plug in that I'm using.
This is the sort of thing im looking for:
$tree->rootNode->{$varString}->title;
where $tree
is the JSON and $varString
can contain a single element (e.g. children[0]
) all the way up to a longer path (e.g. children[0]->children[1]->children[1]
). When I hardcode these keys in, everything works fine, but this wouldnt allow me to generate keys at runtime.
The problem seems to be due to the use of a string to store the key. I can use a variable to store a single object name, but not the index [0] or a path of multiple objects using the ->
operator.
Is there a way of doing this, or will I have to take an entirely different approach?