使用php怎么对树形结构数据进行存取

介绍

本篇文章给大家分享的是有关使用php怎么对树形结构数据进行存取,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

代码如下:

& lt; ?php

/* *
, *树形结构数据存取类
, *,
, *用于对树形结构数据进行快速的存取
, *,
, * @param数组$ arr参数必须为标准的二维数组,包含索引字段(id)与表示树形结构的字段(路径),如示例中所示
, *,
, * @example & lt; code>
, * $ arr=数组(
, *,数组(& # 39;id # 39;=比;1,& # 39;名字# 39;=比;& # 39;php # 39; & # 39; path & # 39;=比;& # 39;1 & # 39;),
, *,数组(& # 39;id # 39;=比;3,& # 39;名字# 39;=比;& # 39;php1& # 39; & # 39; path & # 39;=比;& # 39;1 - 3 # 39;),
, *,数组(& # 39;id # 39;=比;2,& # 39;名字# 39;=比;& # 39;mysql # 39; & # 39; path & # 39;=比;& # 39;2 & # 39;),
, *,数组(& # 39;id # 39;=比;6日& # 39;名字# 39;=比;& # 39;mysql1& # 39; & # 39; path & # 39;=比;& # 39;2 - 6 # 39;),
, *,数组(& # 39;id # 39;=比;7 & # 39;名字# 39;=比;& # 39;mysql2& # 39; & # 39; path & # 39;=比;& # 39;2 - 7日# 39;),
, *,数组(& # 39;id # 39;=比;5,& # 39;名字# 39;=比;& # 39;php11& # 39; & # 39; path & # 39;=比;& # 39;1-3-5& # 39;),
, *,数组(& # 39;id # 39;=比;4,& # 39;名字# 39;=比;& # 39;php2& # 39; & # 39; path & # 39;=比;& # 39;1 - 4 # 39;),
, *,,);
, *,凯特=new美元树(arr);
, *,,
, *,data=https://www.yisu.com/zixun/凯特-> getChild美元(2);
, *,,
, *,print_r ($ data→toArray ());
, * & lt;/code>
, *,
, */
类树
{
,,,public _info美元;,,,,,,,,,,,,,,,,,,,,,,,,,,,,//节点信息
,,,public 数组$ _child=();,,,,,,,,,,,,,,,,,//子节点
,,,私人_parent美元;,,,,,,,,,,,,,,,,,,,,,,,,,,//父节点
,,,私人_data美元;,,,,,,,,,,,,,,,,,,,,,,,,,,,,//当前操作的临时数据
,,,私有静态_indexs 美元;,,,,,,,=()数组;,//所有节点的索引
,,,私有静态_index_key 美元;,,,,=& # 39;id # 39;;,,,,//索引键
,,,私有静态_tree_key 美元;,,,,,=& # 39;path & # 39;;,,//树形结构表达键
,,,私有静态$ _tree_delimiter=& # 39; & # 39;,,,,,,//属性结构表达分割符
,,,,
,,,/* *
,,,,*构造函数
,,,,*,
,,,,* @param数组$ arr
,,,,* @param布尔美元force_sort如果为真,将会强制对美元arr进行排序
,,,,* @return空白
,,,,*/
,,,公共函数__construct(数组$ arr=数组(),,美元force_sort=true)
,,,{
,,,,,,,如果($ force_sort===true) {
,,,,,,,,,,,arr=这→美元_array_sort (arr美元,自我:_tree_key美元);
,,,,,,,}
,,,,,,,如果(! emptyempty (arr)美元){
,,,,,,,,,,,$ this→_init (arr);
,,,,,,,}
,,,}
,,,,
,,,/* *
,,,,*初始存储树形数据
,,,,*,
,,,,* @param数组$ arr
,,,,* @return空白
,,,,*/
,,,私有函数_init(数组$ arr)
,,,{
,,,,,,,foreach (arr项目美元美元){
,,,,,,,,,,,path 美元;,,,,,,=$ item [self:: $ _tree_key];
,,,,,,,,,,,paths 美元;,,,,,美元=爆炸(self:: $ _tree_delimiter路径);
,,,,,,,,,,,$ count_paths=count($路径);
,,,,,,,,,,,parent_id 美元,美元=收取(路径[$ count_paths-2]) ?$路径[$ count_paths-2]:空;
,,,,,,,,,,,,
,,,,,,,,,,,null

使用php怎么对树形结构数据进行存取