Created
January 10, 2018 14:31
-
-
Save vensires/7ea83575660d514a0f58ce0e40c59abb to your computer and use it in GitHub Desktop.
The following function returns the hierarchy of the first-found instance of a key an array. The concept was to take a renderable array, and build the $parents array as required by drupal_array_get_nested_value.
This file contains 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 | |
/** | |
* Returns the hierarchy of the first found instance of a key in an array. | |
*/ | |
function drupal_array_get_nested_key_parents($needle, $haystack, &$parents = array()) { | |
foreach ($haystack as $key => $value) { | |
if ((string) $key == (string) $needle) { | |
$parents[] = $key; | |
return TRUE; | |
} | |
elseif (is_array($value)) { | |
$tempKey = $parents; | |
$tempKey[] = $key; | |
if (drupal_array_get_nested_key_parents($needle, $value, $tempKey)) { | |
$parents = $tempKey; | |
break; | |
} | |
} | |
} | |
return FALSE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment