Last active
December 25, 2015 18:29
-
-
Save IlanFrumer/7020895 to your computer and use it in GitHub Desktop.
sorting array of dependencies
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
function sortByDependencies(array $array){ | |
$solved_dependencies = array(); | |
while(!empty($array)) | |
{ | |
$tmp_list = array(); | |
foreach ($array as $key => $dependencies) { | |
$met = true; | |
foreach ($dependencies as $dependency) { | |
if (! array_key_exists($dependency , $solved_dependencies) ) { $met=false; break;} | |
} | |
if ($met) $tmp_list[$key] = $dependencies; | |
} | |
// more dependencies not found | |
if(empty($tmp_list)) throw new Exception("cannot solve dependencies", 1); | |
$array = array_diff_key($array, $tmp_list); | |
$solved_dependencies = array_merge($solved_dependencies , $tmp_list); | |
} | |
return $solved_dependencies; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment