note 100057 deleted from function.array-search by cmb

From: Date: Sun, 05 Jan 2020 08:20:04 +0000
Subject: note 100057 deleted from function.array-search by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213747@lists.php.net to get a copy of this message
Note Submitter: robert at robert-gonzalez dot com ---- I needed a way to find the parent hierarchy of a multidimensional array. Being the rogue that I am, I got to coding before searching the manual and came up with two little functions that will return a parent stack for a first find and a complete parent stack, similar in nature to the solution presented by jette at nerdgirl dot dk without all the extra stuff or use of eval(). ;) <?php /** * Gets the parent stack of a string array element if it is found within the * parent array * * This will not search objects within an array, though I suspect you could * tweak it easily enough to do that * * @param string $child The string array element to search for * @param array $stack The stack to search within for the child * @return array An array containing the parent stack for the child if found, * false otherwise */ function getParentStack($child, $stack) { foreach ($stack as $k => $v) { if (is_array($v)) { // If the current element of the array is an array, recurse it and capture the return $return = getParentStack($child, $v); // If the return is an array, stack it and return it if (is_array($return)) { return array($k => $return); } } else { // Since we are not on an array, compare directly if ($v == $child) { // And if we match, stack it and return it return array($k => $child); } } } // Return false since there was nothing found return false; } /** * Gets the complete parent stack of a string array element if it is found * within the parent array * * This will not search objects within an array, though I suspect you could * tweak it easily enough to do that * * @param string $child The string array element to search for * @param array $stack The stack to search within for the child * @return array An array containing the parent stack for the child if found, * false otherwise */ function getParentStackComplete($child, $stack) { $return = array(); foreach ($stack as $k => $v) { if (is_array($v)) { // If the current element of the array is an array, recurse it // and capture the return stack $stack = getParentStackComplete($child, $v); // If the return stack is an array, add it to the return if (is_array($stack) && !empty($stack)) { $return[$k] = $stack; } } else { // Since we are not on an array, compare directly if ($v == $child) { // And if we match, stack it and return it $return[$k] = $child; } } } // Return the stack return empty($return) ? false: $return; } // TESTING $array = array( 'balloon' => array( 'red' => array(1 => 'Love', 'Valentine', 'Heart',), 'green' => array(1 => 'Summertime', 'Hope',), ), 'ribbon' => array( 'yellow' => array(2 => 'Welcome',), 'red' => array(3 => 'Love', 'Love',), ), ); $s = getParentStack('Love', $array); $c = getParentStackComplete('Love', $array); var_dump($s, $c); ?> Output: array 'balloon' => array 'red' => array 1 => string 'Love' (length=4) array 'balloon' => array 'red' => array 1 => string 'Love' (length=4) 'ribbon' => array 'red' => array 3 => string 'Love' (length=4) 4 => string 'Love' (length=4)

« previous php.notes (#213747) next »