0

好的,这是范围(必须从内存中背诵它,希望它有意义):具有“函数”的数组需要在它们的出现和返回值之后进行排序,以便您可以看到函数的顺序,它们需要“加载”及其“依赖项”。

不应该有重复,空返回应该被忽略

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");

结果数组应如下所示

$result = array(
    "human" => array(
        "worker" => array(),
        "clerk" => array(
            "manager" => array(),
        ),
    ),
    "animal" => array()
);

所以:首先需要“加载”人类,然后是工人、职员、经理和动物。工人和文员靠人,经理靠文员。

到目前为止,我得到了这个:

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

    if(!empty($returnvallue) && !in_array($returnvallue, $result))
    {
        $result[$returnvallue] = array();
    }
}

这给了我这个数组

$result = array(
    "human" => array(),
    "worker" => array(),
    "clerk" => array(),
    "manager" => array(),
    "animal" => array()
);

但它只向我展示了发生的顺序。

我难住了。

4

1 回答 1

0
<?  
function human() 
{
    return "";
}
function animal()
{
    return "";
}
function worker() 
{
    return "human";
} 
function clerk() 
{
    return "human";
} 
function manager() 
{
    return "clerk";
}
function bird() 
{
    return "animal";
}
function coworker() 
{
    return "worker";
}
function things()
{
    return "";
}   
$task = array("bird","animal","manager","human", "worker","things","human", "worker", "clerk", "animal","coworker");

$result = array();
foreach($task as $function)
{
    $tmp=array();
    $tmp[]=$function;
    $returnvalue = $function();
    if(!empty($returnvalue)) 
    {

        $tmp[]=$returnvalue;
        while(function_exists($returnvalue) && $returnvalue()<>"")
        {
            $tmp[]=$returnvalue=$returnvalue();

        }   
    }
    $tmp2 =& $result;   
    foreach (array_reverse($tmp) as $key)
    {
        if(!isset($tmp2[$key]))$tmp2[$key]=array();
        $tmp2 =& $tmp2[$key];
    }     
}
于 2013-05-31T00:01:38.677 回答