java
php
iphone
css
linux
xcode
android
objective-c
silverlight
flash
perl
algorithm
facebook
tsql
delphi
mvc
asp
jsp
postgresql
dom
function walknode($node) { //Do some stuff with the node here, e.g. echo "I am working on node $node->name<br>\n"; if (is_array($node->children)) foreach ($node->children as $child) walknode($child); } walknode($basenode);
If nodes of all generations have distinct IDs, this ought to work:
$idArray = array(); $nodes = $node->children(); foreach ($nodes as $pKey => $parent) { array_push($idArray,$pKey); $childNodes = $parent->children(); foreach ($childNodes as $cKey => $child) { array_push($idArray,$cKey); } }
Try something like the following:
function walkNodes($node, $props = array()) { $props[] = $node->id; if(isset($node->children) && is_array($node->children)){ foreach($node->children as $child) { $props = walkNodes($child, $props); } } return $props; }
Assign a method to the class these objects are instances of, something like hasChildren. If during iteration of array_keys($children), one of the children returns true then you have to traverse into it.
<?php class SomeCollection { public $name; public $children = array(); public function hasChildren() { return !empty($this->children); } public function iterate() { // process children foreach(array_keys($this->children) as $child) { // process grandchildren if($child->hasChildren()) { foreach(array_keys($child->children) as $grandchild) { echo $child . ' is my child & ' . $grandchild . ' is my grandchild!' . PHP_EOL; } } else // process children without grandchildren echo $child . ' is my child of mine with no children of his own!'; } } }
If you want to explore some built in tools checkout the SPL Iterators
When i understand your question correctly u get a list of the keys by:
array_keys($node->children)
for iterating use
for ($node->children as $key => $value) { var_dump($key . ' => ' . $value); }