Last active
August 29, 2015 14:16
-
-
Save morozow/5c10d9f2af6d6fa9f789 to your computer and use it in GitHub Desktop.
This recurcive function build full path for nested set node by classic array
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 | |
/** | |
* This recurcive function build full path for nested set node by classic array | |
* | |
* @param $tree | |
* @param $needle | |
* @param array $path | |
* @return array|bool | |
*/ | |
private function getAncestorsByTree($tree, $needle, $path = []) { | |
foreach ($tree as $item) { | |
$path[] = $item['code']; | |
if (!empty($item['__children']) && $needle !== $item['code']) { | |
$ancestorsFound = $this->getAncestorsByTree($item['__children'], $needle, $path); | |
if ($ancestorsFound !== false) { | |
return $ancestorsFound; | |
} else { | |
array_pop($path); | |
} | |
} elseif (empty($item['__children']) && $needle !== $item['code']) { | |
array_pop($path); | |
} elseif ($needle === $item['code']) { | |
return $path; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment