Created
September 16, 2014 07:39
-
-
Save Ikke/22533254d99cb9d395fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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