Skip to content

Instantly share code, notes, and snippets.

@Ikke
Created September 16, 2014 07:39
Show Gist options
  • Save Ikke/22533254d99cb9d395fc to your computer and use it in GitHub Desktop.
Save Ikke/22533254d99cb9d395fc to your computer and use it in GitHub Desktop.
<?php
function get_path($obj, $path, $default=NULL)
{
if(empty($obj)) {
return $default;
}
$parts = explode(".", $path);
$current_part = array_shift($parts);
$current_obj = $obj;
while(!empty($current_part) || $current_part === "0") {
if(is_object($current_obj) && !empty($current_obj->$current_part)) {
$current_obj = $current_obj->$current_part;
} elseif(is_array($current_obj) && !empty($current_obj[$current_part])) {
$current_obj = $current_obj[$current_part];
} else {
return $default;
}
$current_part = array_shift($parts);
}
return $current_obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment