【发布时间】:2016-09-13 22:55:55
【问题描述】:
假设我有一个这样的数组
Array(
Array("id_1" => 1,"id_2" => 1,"name" => "test1","type" => "A","ref_1" => 0,"ref_2" => 0,),
Array("id_1" => 1,"id_2" => 2,"name" => "test2","type" => "B","ref_1" => 1,"ref_2" => 1,),
Array("id_1" => 1,"id_2" => 3,"name" => "test3","type" => "B","ref_1" => 1,"ref_2" => 1,),
Array("id_1" => 2,"id_2" => 1,"name" => "test4","type" => "B","ref_1" => 1,"ref_2" => 1,),
Array("id_1" => 2,"id_2" => 3,"name" => "test5","type" => "C","ref_1" => 1,"ref_2" => 2,),
Array("id_1" => 2,"id_2" => 15,"name" => "test6","type" => "C","ref_1" => 1,"ref_2" => 3,),
Array("id_1" => 5,"id_2" => 22,"name" => "test7","type" => "B","ref_1" => 4,"ref_2" => 9,),
Array("id_1" => 4,"id_2" => 9,"name" => "test8","type" => "C","ref_1" => 1,"ref_2" => 1,),
Array("id_1" => 1,"id_2" => 7,"name" => "test9","type" => "C","ref_1" => 2,"ref_2" => 1,),
Array("id_1" => 5,"id_2" => 20,"name" => "test10","type" => "B","ref_1" => 4,"ref_2" => 9,),
Array("id_1" => 5,"id_2" => 5,"name" => "test11","type" => "B","ref_1" => 4,"ref_2" => 9,),
Array("id_1" => 5,"id_2" => 4,"name" => "test12","type" => "B","ref_1" => 1,"ref_2" => 1,),
);
“id_1”和“id_2”两个“主键”,两者的组合不能重复。每一行都可以被另一个使用“ref_1”和“ref_2”引用,被引用的行将包含来自referer(父亲)的“id_1”和“id_2”。
无法引用 A 类行。类型 B 和 C 可以被 A 引用。同样,B 可以被 C 引用,反之亦然。树并不总是完整的,它可能只有 TYPE A 和 B 或 TYPE A 和 C,或者只有 TYPE A。
我正在尝试创建一个返回如下表格的函数:
| TYPE A NAME | TYPE B NAME | TYPE C NAME |
| ------------------------------------------- |
| test1 | test2 | test5 |
| test1 | test3 | test6 |
| test1 | test7 | test8 |
| test1 | test4 | test9 |
| test1 | test10 | test8 |
| test1 | test11 | test8 |
| test1 | test12 | |
每行引用 2 个(或更多)其他行,数据是父亲的数据被拆分并为每个引用输出一行。如果一个 A 类引用 2 个 B 类,每个 B 类引用其他 2 个 C 类,则输出表将有 4 行。
不止三种,这只是一个PoC。我试图尽可能详细地解释这一点,但可能有点难以理解。我知道它可能涉及一些递归,但我在过去 8 小时里一直在尝试,但无法完成这项工作。
PS:输出也可以是一个数组。
如果需要任何进一步的信息,请告诉我。
【问题讨论】: