Skip to content

Instantly share code, notes, and snippets.

@altbdoor
Created June 13, 2014 02:07
Show Gist options
  • Save altbdoor/157079c9e9ce86fc51c1 to your computer and use it in GitHub Desktop.
Save altbdoor/157079c9e9ce86fc51c1 to your computer and use it in GitHub Desktop.
check circular referencing in flat array in PHP
private static function isHierarchyCircular ($hierarchy, $circularIdentifier, $nextIdentifier) {
if (!array_key_exists($nextIdentifier, $hierarchy)) {
$hierarchy[$nextIdentifier] = array();
}
$startHierarchy = $hierarchy[$nextIdentifier];
if (in_array($circularIdentifier, $startHierarchy)) {
return true;
}
else {
foreach ($startHierarchy as $subStartHierarchy) {
if (self::isHierarchyCircular($hierarchy, $circularIdentifier, $subStartHierarchy)) {
return true;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment