Skip to content

Instantly share code, notes, and snippets.

@devinus
Created October 28, 2011 21:59
Show Gist options
  • Save devinus/1323694 to your computer and use it in GitHub Desktop.
Save devinus/1323694 to your computer and use it in GitHub Desktop.
<?php
function getPath($target, $path, $default=null) {
if (!$target) return $default;
if (!$path) return $target;
$parts = explode('.', $path);
while (list($idx, $key) = each($parts)) {
if (isset($target->$key)) {
$target = $target->$key;
continue;
}
elseif (is_array($target)) {
if ($key === '@each') {
$acc = array();
foreach ($target as $t) {
$subpath = join('.', array_slice($parts, $idx+1));
$acc[] = (array) getPath($t, $subpath);
}
return call_user_func_array('array_merge', $acc);
}
elseif (isset($target[$key])) {
$target = $target[$key];
continue;
}
}
return $default;
}
return $target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment