Created
June 13, 2014 02:07
-
-
Save altbdoor/157079c9e9ce86fc51c1 to your computer and use it in GitHub Desktop.
check circular referencing in flat array in PHP
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
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