好吧,这是范围(必须从内存中背诵它,希望它有意义):带有“函数”的数组需要在它们出现和返回值后进行排序,以便您可以看到函数的顺序,他们需要“加载”和他们的“依赖”。
应该没有重复项,应忽略空回报
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()
);
但它只显示了出现的顺序。
我很难过。
答案 0 :(得分: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];
}
}