I have to cache a tree structure and access it later on. Problem: I really can't figure out how to declare the data so that it fits to RecursiveIteratorIterator
etc. It's probably a very n00bish question, but I have tried lots of combinations and ran out of ideas :-(
Conceptually the data looks like this:
ROOT code : 1111, label : Universe
- code : 2000, label : Asia
- code : 3203, label : Hongkong
-code : 2081, label: Greater Area
-code : 2041, label: Downtown
- code : 4020, label : Shanghai
- code : 6201, label : Africa
- code : 321, label : North America
I want to access all direct childs to a given code, e.g. for Asia Hongkong and Shanghai.
RecursiveIteratorIterator
seems to make this easy.
// looking for Asia with code = 2000
$iterator = new RecursiveIteratorIterator(new Universe_Tree($tree));
foreach ($iterator as $key => $item) {
if ($item->code == 2000) {
var_dump($iterator->callGetChildren());
}
}
Class Universe_Tree does not do much yet:
class Universe_Tree extends ArrayIterator implements RecursiveIterator {
public function hasChildren() {
return (is_array($this->current()));
}
public function getChildren() {
return new self($this->current());
}
}
My best approach was to create objects of each node and store them in a nested array
$universe = new stdClass();
$universe ->code = 1111;
$universe ->label = "Universe";
$tree = array(
array($universe,
array(
$asia,
(array($shanghai,$hongkong)),
$europe
// and so on
)
);
Unfortunately $iterator->callGetChildren()
does not return the children, just the current element. Probably because the nodes are not nested together correctly. I also tried to nest arrays with a parentId but this lead to an error message from ArrayIterator
that this is not an array or object, though according to var_dump
it was an array. What else could I try?