Friday, February 6, 2009, 2:16:15 AM, Patrick wrote: > What happens with...?
> a:b:c = val1 > a:b = val2 > It would seem that array[a][b] can't simultaneously be a value and an array. True. I would probably want to check if a value (or array) exists before setting a new value, so it just gets added at the right level. I like to thank everyone for their ideas! None worked for me, but I got inspired by each response, and took something from it! I think I got a solution I can share here now: The functions make it possible to pass the keys either as array or as string with ':' separators between the words. For getting a node value the function cuts the array tree iteratively smaller till the leaf node is left: function TreeNodeGet($keys, $tree) { if (!$keys) return null; if (!is_array($keys)) $keys = explode(":", $keys); while ($key = array_shift($keys)) { if (!array_key_exists($key, $tree)) return null; $tree = $tree[$key]; //reduce branch iteratively } return implode("",$tree); //node leaf } //}}} But I could not figure a way to use iterations for setting a new value, I kept messing up the original array. So here I am using eval, which should not pose problems with input restricted to safe characters. This function will just overwrite an existing value: function TreeNodeSet($keys, &$tree, $val) { if (is_array($keys)) $keys = implode(":", $keys); return eval( '$tree[\''.str_replace(':', '\'][\'', $keys).'\'] = $val; return true;'); } //}}} Then to add some checks for preventing overwriting two more functions, also using eval to pass the keys: function TreeNodeExists($keys, $tree) { if (is_array($keys)) $keys = implode(":", $keys); return eval( 'if(isset($tree[\''.str_replace(':', '\'][\'', $keys).'\'])) return true; else return false;'); } //}}} function TreeNodeIsArray($keys, $tree) { if (is_array($keys)) $keys = implode(":", $keys); return eval( 'if(is_array($tree[\''.str_replace(':', '\'][\'', $keys).'\'])) return true; else return false;'); } //}}} There are probably better php solutions out there. But stuff I found about iteration classes I could not understand. Most info seems to be about accessing or writing hierarchical data structures to a SQL db. Hans _______________________________________________ pmwiki-devel mailing list pmwiki-devel@pmichaud.com http://www.pmichaud.com/mailman/listinfo/pmwiki-devel