当前位置:网站首页>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 ",
],
];
边栏推荐
- Ingress Controller 0.47.0的Yaml文件
- Explanation of suffix of Oracle EBS standard table
- 解决万恶的open failed: ENOENT (No such file or directory)/(Operation not permitted)
- ORACLE EBS ADI 开发步骤
- Spark的原理解析
- ORACLE 11G SYSAUX表空间满处理及move和shrink区别
- ERNIE1.0 与 ERNIE2.0 论文解读
- Message queue fnd in Oracle EBS_ msg_ pub、fnd_ Application of message in pl/sql
- 第一个快应用(quickapp)demo
- @Transitional step pit
猜你喜欢

MapReduce与YARN原理解析

【论文介绍】R-Drop: Regularized Dropout for Neural Networks

MapReduce concepts and cases (Shang Silicon Valley Learning Notes)

ssm人事管理系统

oracle apex ajax process + dy 校验

Sqli-labs customs clearance (less2-less5)

sparksql数据倾斜那些事儿

SSM二手交易网站

图解Kubernetes中的etcd的访问

【模型蒸馏】TinyBERT: Distilling BERT for Natural Language Understanding
随机推荐
一个中年程序员学习中国近代史的小结
ORACLE EBS接口开发-json格式数据快捷生成
Oracle APEX 21.2 installation et déploiement en une seule touche
Oracle segment advisor, how to deal with row link row migration, reduce high water level
ORACLE APEX 21.2安裝及一鍵部署
DNS attack details
php中的数字金额转换大写数字
Ceaspectuss shipping company shipping artificial intelligence products, anytime, anywhere container inspection and reporting to achieve cloud yard, shipping company intelligent digital container contr
Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix
Message queue fnd in Oracle EBS_ msg_ pub、fnd_ Application of message in pl/sql
Basic knowledge of software testing
【Ranking】Pre-trained Language Model based Ranking in Baidu Search
Ingress Controller 0.47.0的Yaml文件
MapReduce concepts and cases (Shang Silicon Valley Learning Notes)
Network security -- intrusion detection of emergency response
Module not found: Error: Can't resolve './$$_ gendir/app/app. module. ngfactory'
ERNIE1.0 与 ERNIE2.0 论文解读
離線數倉和bi開發的實踐和思考
SSM second hand trading website
ORACLE 11.2.0.3 不停机处理SYSAUX表空间一直增长问题