我在数据库中有与父母有关的人,我需要创建递归函数,该函数返回带有家谱树的数组。数据库示例:
NUMBER;NAME;FATHER;MOTHER
001;Name1;002;005
002;Name2;007;018
003;Name3;018;025
005;Name5;006;019
023;Name23;019;045
018;Name18;062;097
007;Name7;;-
...
我准备了两个函数——第一个用于人的详细信息,第二个用于搜索祖先的递归。
我需要这个结果:
$out[0][0] // first person
$out[1][0] // mother of first person
$out[1][1] // father of first person
$out[2][0] // grandmother of first person (mother side)
$out[2][1] // grandfather of first person (mother side)
$out[2][2] // grandmother of first person (father side)
$out[2][3] // grandmother of first person (father side)
...
下一代有 8 个项目,接下来的 16 个项目,... 最多是 6 代回来。
我准备了两个函数——第一个用于人员细节,第二个用于树构建。在第二个功能
define("MAX_GEN",5);
function detail($number) {
$d = mysql_query("select * from table where number = '$number'");
if(mysql_num_rows($d) == 0) {
$p[name] = "N/A";
$p[number] = "N/A";
$p[m_number] = "N/A";
$p[f_number] = "N/A";
}
else $p = mysql_fetch_assoc($d);
return $p;
}
function gen($number, $generation = 0, $out) {
if ($generation >= MAX_GEN) {
return false;
}
$record = detail($number);
if ($generation == 0) $out[0][] = $record; // first man
$generation++; // next generation
if (!$out[$generation] && ($generation != MAX_GEN)) $out[$generation] = array();
$x_mother = gen($record[m_number], $generation ); // continue with mother
$x_father = gen($record[f_number], $generation ); // continue with father
if ($out[$generation]) {
$out[$generation][] = $x_mother;
$out[$generation][] = $x_father;
}
return $out;
}
但是在第二个函数中是问题 - 我不知道如何将带有结果的数组传递给下一代和下一代。它仍然返回一个元素,或者(经过几次尝试)它返回我想要的数组,但是在几代之间插入了数组,结果不可用。任何人都可以帮助我吗?
我想要的结果示例在这里:http ://www.foxterrier.cz/_TM_importy/example.php