Skip to content

Instantly share code, notes, and snippets.

@morozow
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morozow/5c10d9f2af6d6fa9f789 to your computer and use it in GitHub Desktop.
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
<?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