Skip to content

Instantly share code, notes, and snippets.

@IlanFrumer
Last active December 25, 2015 18:29
Show Gist options
  • Save IlanFrumer/7020895 to your computer and use it in GitHub Desktop.
Save IlanFrumer/7020895 to your computer and use it in GitHub Desktop.
sorting array of dependencies
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