Sort an array of "functions" after their occurance and return value
<p>Ok, this is the scope (have to recite it from memory, hope it makes sense): An array with "functions" needs to be sorted after their occurrence and return value, so that you can see the order of the functions, which they need to be "loaded" and their "dependencies".</p>

<p>There should be no duplicates and empty returns should be ignored</p>

<pre><code>function human() 
{
    return "";
}
function animal()
{
    return "";
}
function worker() 
{
    return "human";
} 
function clerk() 
{
    return "human";
} 
function manager() 
{
    return "clerk";
}

$task = array("human", "worker", "human", "worker", "clerk", "manager", "animal");
</code></pre>

<p>The resulting array should look like this</p>

<pre><code>$result = array(
    "human" =&gt; array(
        "worker" =&gt; array(),
        "clerk" =&gt; array(
            "manager" =&gt; array(),
        ),
    ),
    "animal" =&gt; array()
);
</code></pre>

<p>So: human needs to be "loaded" first, then worker, clerk, manager and animal.
worker and clerk depend on human, manager depends on clerk.</p>

<p>So far, I got this:</p>

<pre><code>$result = array();
foreach($task as $function)
{
    if(!in_array($function, $result))
    {
        $result[$function] = array();
    }
    $returnvallue = $function();

    if(!empty($returnvallue) &amp;&amp; !in_array($returnvallue, $result))
    {
        $result[$returnvallue] = array();
    }
}
</code></pre>

<p>Which gives me this array</p>

<pre><code>$result = array(
    "human" =&gt; array(),
    "worker" =&gt; array(),
    "clerk" =&gt; array(),
    "manager" =&gt; array(),
    "animal" =&gt; array()
);
</code></pre>

<p>But it only shows me the order of occurance.</p>

<p>I'm stumped.</p>
