Skip to content

Instantly share code, notes, and snippets.

@brycefisher
Last active December 25, 2015 00:29
Show Gist options
  • Select an option

  • Save brycefisher/6888274 to your computer and use it in GitHub Desktop.

Select an option

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.
<?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&#x005B;'$prop'&#x005D; = " . htmlentities($value);
break;
case 'object':
case 'array':
_find_deep_needle($value, $needle, "$php_exp&#x005B;'$prop'&#x005D;");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment