这里有一些非递归代码可以让你开始(如果你还没有解决它),它将根据从电子表格加载的$rows数组构建一个树。

这个想法是每个节点都有一个名称和一个子数组。所以代码只是在步骤1中为每个人(父和子)创建一个节点,然后从下到上填写步骤2中的链接。

代码不健壮,如果$rows的行重复,列表中的顺序不正确,或者在错误的地方显示孩子和父母,则可能会产生不必要的结果!良好的生产代码需要处理这些可能性,可能是在构建树之前检查和修复$rows。

$rows = [];

$rows[] = array(0,1);

$rows[] = array(1,2);

$rows[] = array(1,3);

$rows[] = array(1,4);

$rows[] = array(2,5);

$rows[] = array(3,6);

$rows[] = array(6,7);

// Build the required tree

$tree = makeTree($rows);

print_r($tree);

function makeTree(array $rows){

//----------------------

// Step 1. Make a list of nodes

// -----------------------

// make the parent node

$nodeList =[];

$nodeList[0]['name'] = "parent:";

$nodeList[0]['Children'] = [];

// make the child nodes

foreach ($rows as $cells)

{

$nodeList[$cells[1]]['name'] = "child:".$cells[1];

$nodeList[$cells[1]]['Children'] = [];

}

//----------------------

// Step 2. link each child node to its parent node

// -----------------------

for ($n = count($rows)-1; $n>=0; $n--)

{ // do this from the bottom up

$nodeParent = &$nodeList[$rows[$n][0]];

$nodeChild = &$nodeList[$rows[$n][1]];

$nodeParent['Children'][$rows[$n][1]]= $nodeChild;

}

// pick out the parent node (which by now should have all links in place)

$tree[0] = $nodeList[0];

return($tree);

}

它输出如下,可能或不接近你需要的。

Array

(

[0] => Array

(

[name] => parent:

[Children] => Array

(

[1] => Array

(

[name] => child:1

[Children] => Array

(

[4] => Array

(

[name] => child:4

[Children] => Array

(

)

)

[3] => Array

(

[name] => child:3

[Children] => Array

(

[6] => Array

(

[name] => child:6

[Children] => Array

(

[7] => Array

(

[name] => child:7

[Children] => Array

(

)

)

)

)

)

)

[2] => Array

(

[name] => child:2

[Children] => Array

(

[5] => Array

(

[name] => child:5

[Children] => Array

(

)

)

)

)

)

)

)

)

)

php 合并数组成父子关系,php - 将电子表格解析为PHP数组并返回具有父子关系的嵌套MLM表 - SO中文参考 - www.soinside.com...相关推荐

  1. java数组里套数组_用JAVA进行Json数据解析(对象数组的相互嵌套)

    这段时间我们在做一个英语翻译软件的小小小APP,涉及到了对Json数据的解析,所以特地来总结一下! 假设我们要对如下数据进行解析,其实在平时,返回的Json数据是很乱的,很难分清数据的关系,这是经过相 ...

  2. php 覆盖相同键的值,php - PHP数组使用相同的字符串键组合值 - SO中文参考 - www.soinside.com...

    我有以下常见问题,但无法确定哪种数组函数可能适用于以下格式: ((已尝试array_merge,array_merge_recursive,array_combine,array_splice,但未按 ...

  3. php添加数组的键和值,php - 将php数组元素添加到数组中如何具有相同的键和值 - SO中文参考 - www.soinside.com...

    我有一个像这样的数组如何具有相同的gtin:Array ( [0] ( [status] => ERROR [message] => 'message error' [gtin] => ...

  4. vue中数组长度_vue.js - 如何获得动态数组的数组长度 - SO中文参考 - www.soinside.com...

    您知道从API加载所有包含的数据后如何获取数字行问题数组.我需要为分页表设置totalRows,但是this.questions = this.questions.lenght 无效,返回null.e ...

  5. python数组转换为列表_python - 将一系列数组转换为单个列表 - SO中文参考 - www.soinside.com...

    我需要将数组中具有数组的序列转换为列表.这是系列:0 0 [[136.26198653744652]] 1 [[595.1701354429704]] 2 [[106.31607570796812]] ...

  6. php如何输出关联数组的值,php - 如何从PHP关联数组中获取确切的输出 - SO中文参考 - www.soinside.com...

    我试图在关联数组上使用foreach循环创建一个html表.这是关于数组的更多细节. $assoc_array = array( "0" => array( "pr ...

  7. java pdf无法加载_java - 试图使用iText7合并来合并pdf,但是当我打开最终的合并pdf时,它说无法加载pdf文档 - SO中文参考 - www.soinside.com...

    试图使用iText7合并来合并pdf,但是当我打开最终的合并pdf时,它说无法加载pdf文档 问题描述 投票:0回答:1 我正在使用itext7 PdfWriter创建两个ByteArrayOutpu ...

  8. android数据截取字符串数组,android - 从Android Studio中的字符串数组获取特定字符串 - SO中文参考 - www.soinside.com...

    我有一个字符串数组(在strings.xml中,我在其中收集所有游戏) ... Fortnite CounterStrike: Global Offensive Minecraft Minigames ...

  9. php取出多维数组的所有元素,php - 获取多维数组PHP中两个元素之间的所有数组 - SO中文参考 - www.soinside.com...

    [我需要帮助.我有PHP困难.这是多维数组,我想将"信贷协议摘要"数组和"信贷协议"数组之间的所有数组完全放入一个单独的数组中.我试图使用range()函数,但 ...

最新文章

  1. xtrabackup 使用说明(续)
  2. 【STM32】MDK5打开MDK4项目工程时出现的不兼容及解决方法
  3. Oracle入门(十四.13)之带参数的游标
  4. 例子:Basic Lens sample
  5. P1009 [NOIP1998 普及组] 阶乘之和-2022.02.01(python3实现)
  6. Unable to load print control in ReportingService
  7. maven jar包下载
  8. jqgrid 行选中multiboxonly属性说明
  9. 读计算机专业买什么笔记本电脑好?你算问对人了
  10. socketException之broken pipe
  11. php ios表情包,[iOS] 自定义表情包
  12. 使用Zack.EventBus 对rabbitMQ简化操作
  13. Golang学习——error错误处理浅谈
  14. 抢滩登陆瑞星杀毒2005(转)
  15. 关于dubbo快速开发和服务提供者无法注册上注意点
  16. 三菱FX系列PLC以太网连接力控ForceControl
  17. [LuoguU41039]PION后缀自动机 树链剖分+动态开点线段树
  18. Win11电脑一边耳机没声音怎么解决
  19. 腾讯物联网系统TOS,内核移植起来比你想象的要简单
  20. Win10 自定义右键新建菜单

热门文章

  1. java 实现nfa的化简_DFA与NFA的等价性,DFA化简
  2. pat 乙级 1012 数字分类(C++)
  3. 【渝粤教育】 国家开放大学2020年春季 2071美学与美育 参考试题
  4. 【渝粤教育】广东开放大学 财政学 形成性考核 (43)
  5. php vimrc配置文件,vim技巧:我的 .vimrc 配置文件,详解每一个配置项的作用
  6. php 长微博程序,长微博生成工具PHP源码 根据文字生成长微博图片 - substr_count
  7. mfc中怎么集成文件_怎么把几个pdf合并并打印在一份文件中?
  8. python集合用法_Python 集合(Set)
  9. 浙江计算机二级报名步骤,浙江2016年9月计算机二级考生报名流程
  10. android 设置资源,Android 资源