当前位置:网站首页>Transform the tree structure into array in PHP (flatten the tree structure and keep the sorting of upper and lower levels)
Transform the tree structure into array in PHP (flatten the tree structure and keep the sorting of upper and lower levels)
2022-07-02 07:20:00 【Snow wind in the night sky】
Preface
Doing it php In the function of transforming tree structure into array, sometimes it is necessary to directly flatten the tree structure and arrange it from top to bottom in the order of tree structure , At this time, another method is needed .
terms of settlement
// Tree structure data to array ( Flatten tree structure , Keep superior and subordinate sorting )
function treeToList($arr, $pid = 0)
{
$array = array();
foreach ($arr as $val) {
$inData = array(
"key" => $val["key"],
"p_key" => $pid,
"title" => $val["title"],
);
$array[] = $inData;
if (isset($val["children"])) {
$children = treeToList($val["children"], $val["key"]);
if ($children) {
$array = array_merge($array, $children);
}
}
}
return $array;
}
Directly flatten the tree structure , Keep sorting up and down .
$tree = [
[
"key" => 1,
"p_key" => 0,
"title" => " Hubei province ",
"children" => [
[
"key" => 2,
"p_key" => 1,
"title" => " Wuhan City ",
"children" => [
[
"key" => 3,
"p_key" => 2,
"title" => " Wuchang District ",
"children" => [
[
"key" => 4,
"p_key" => 3,
"title" => " Sheshan Xishan pote 1 Number ",
"children" => [
[
"key" => 5,
"p_key" => 4,
"title" => " Yellow Crane Tower ",
],
],
],
],
],
],
],
],
],
];
$data = [
[
"key" => 1,
"p_key" => 0,
"title" => " Hubei province ",
],
[
"key" => 2,
"p_key" => 1,
"title" => " Wuhan City ",
],
[
"key" => 3,
"p_key" => 2,
"title" => " Wuchang District ",
],
[
"key" => 4,
"p_key" => 3,
"title" => " Sheshan Xishan pote 1 Number ",
],
[
"key" => 5,
"p_key" => 4,
"title" => " Yellow Crane Tower ",
],
];
边栏推荐
- ERNIE1.0 与 ERNIE2.0 论文解读
- view的绘制机制(三)
- Three principles of architecture design
- RMAN incremental recovery example (1) - without unbacked archive logs
- Oracle EBS ADI development steps
- Explain in detail the process of realizing Chinese text classification by CNN
- 【Torch】解决tensor参数有梯度,weight不更新的若干思路
- 使用 Compose 实现可见 ScrollBar
- ORACLE APEX 21.2安裝及一鍵部署
- The boss said: whoever wants to use double to define the amount of goods, just pack up and go
猜你喜欢
Practice and thinking of offline data warehouse and Bi development
Sqli-labs customs clearance (less2-less5)
Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
IDEA2020中PySpark的两表关联(字段名相同)
Yolov5 practice: teach object detection by hand
ORACLE EBS中消息队列fnd_msg_pub、fnd_message在PL/SQL中的应用
Go package name
Analysis of MapReduce and yarn principles
TCP攻击
随机推荐
图解Kubernetes中的etcd的访问
读《敏捷整洁之道:回归本源》后感
Oracle rman自动恢复脚本(生产数据向测试迁移)
TCP attack
类加载器及双亲委派机制
Oracle RMAN automatic recovery script (migration of production data to test)
Oracle APEX 21.2 installation et déploiement en une seule touche
Yolov5 practice: teach object detection by hand
Illustration of etcd access in kubernetes
Oracle EBS DataGuard setup
php中在二维数组中根据值返回对应的键值
oracle apex ajax process + dy 校验
Explain in detail the process of realizing Chinese text classification by CNN
php中树形结构转数组(拉平树结构,保留上下级排序)
php中通过集合collect的方法来实现把某个值插入到数组中指定的位置
【信息检索导论】第一章 布尔检索
ORACLE EBS 和 APEX 集成登录及原理分析
解决万恶的open failed: ENOENT (No such file or directory)/(Operation not permitted)
Basic knowledge of software testing
Oracle 11g uses ords+pljson to implement JSON_ Table effect