I want to access a specific array\'s property by using a separate array as the path. The problem is the property in question may be at any depth. Here\'s an example...
Here is a different approach:
while($d = array_shift($path))
$data = $data[$d];
A simple loop should work:
Update: sorry did check my code
foreach($path as $id)
{
$data = $data[$id];
}
echo $data;
Result:
deeper_value
This will overwrite the $data
array so you might want to make a copy of $data
first like David did in his example.
It's not the greatest code, but should work:
function getValue($pathArray, $data)
{
$p = $data;
foreach ($pathArray as $i)
{
$p = $p[$i];
}
return $p;
}