Ok ... this is quite a tough one i think ... I've been pondering over it for quite a while now.
Basically, what i want to do is allow someone to access an infinitely nested number of arrays by specifying a "path"
(like to access greetings->hello in :
:
array('greetings' => array('hello' => "Hiya"));
you would use the path 'greetings->hello')
... that can be done with recursion easily with code like
function _getTarget($path, &$currentBranch)
{
$retVal = null;
if(is_array($currentBranch))
{
$current = array_shift($path);
if(isset($currentBranch[$current]))
$retVal = $this->_getTarget($path, $currentBranch[$current]);
}
else
$retVal = $currentBranch;
return $retVal;
}
Simply pass in the root branch on the first calland it works a treat...
HOWEVER, instead of returning the value I want it to return a reference.
Simply changing the $retVal assignments to reference assignments doesn't work because when the function returns from the last level of recursion the reference is invalidated becasue it was in the top level of recursions function scope.
It's quite a mind bender and I'm beginning to think it isn't possiblewithout writing a custom array implementation in PHP which would be intensely slow 🙁
Hope it makes sense because it's qutie hard to explain.
Any comments or theories (or even solutions 😃) are much appreciated.