Last active
December 25, 2015 00:29
-
-
Save brycefisher/6888274 to your computer and use it in GitHub Desktop.
Like var_dump, but it prints out the whole expression "path" to the given string.
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 | |
| /** | |
| * Like var_dump, but it prints out the whole expression "path" to the given string. | |
| * | |
| * @param mixed $haystack | |
| * The object or array with many levels of nested arrays or objects containing your needle | |
| * @param str $php_exp | |
| * The php expression for the initial $haystack param | |
| * @return str | |
| * No return value. Prints output to stdout (assumed to be a browser). | |
| */ | |
| function find_deep_needle($haystack, $php_exp = '$node') { | |
| if (gettype($haystack) == 'object') { | |
| $props = get_object_vars($haystack); | |
| foreach($props as $prop => $value) { | |
| switch(gettype($value)) { | |
| case 'string': | |
| print "<br>$php_exp->$prop = " . htmlentities($value); | |
| break; | |
| case 'object': | |
| case 'array': | |
| _find_deep_needle($value, $needle, "$php_exp->$prop"); | |
| break; | |
| } | |
| } | |
| } elseif (gettype($haystack) == 'array') { | |
| foreach($haystack as $prop => $value) { | |
| switch(gettype($value)) { | |
| case 'string': | |
| print "<br>$php_exp['$prop'] = " . htmlentities($value); | |
| break; | |
| case 'object': | |
| case 'array': | |
| _find_deep_needle($value, $needle, "$php_exp['$prop']"); | |
| break; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment