当前位置:网站首页>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 ",
],
];
边栏推荐
- Practice and thinking of offline data warehouse and Bi development
- ORACLE APEX 21.2安装及一键部署
- php中计算树状结构数据中的合计
- php中删除指定文件夹下的内容
- Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
- view的绘制机制(三)
- 解决万恶的open failed: ENOENT (No such file or directory)/(Operation not permitted)
- sqli-labs通關匯總-page2
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
- 搭建frp进行内网穿透
猜你喜欢

@Transitional step pit

Network security -- intrusion detection of emergency response

如何高效开发一款微信小程序

使用Matlab实现:Jacobi、Gauss-Seidel迭代

Cognitive science popularization of middle-aged people

The boss said: whoever wants to use double to define the amount of goods, just pack up and go

【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization

MapReduce与YARN原理解析

Check log4j problems using stain analysis

叮咚,Redis OM对象映射框架来了
随机推荐
oracle apex ajax process + dy 校验
allennlp 中的TypeError: Object of type Tensor is not JSON serializable错误
使用Matlab实现:弦截法、二分法、CG法,求零点、解方程
Sqli Labs clearance summary - page 2
【Ranking】Pre-trained Language Model based Ranking in Baidu Search
CAD secondary development object
Oracle APEX 21.2 installation et déploiement en une seule touche
Oracle rman半自动恢复脚本-restore阶段
Yolov5 practice: teach object detection by hand
CRP implementation methodology
php中判断版本号是否连续
架构设计三原则
CSRF attack
MySQL组合索引加不加ID
php中根据数字月份返回月份的英文缩写
view的绘制机制(三)
Oracle 11g sysaux table space full processing and the difference between move and shrink
Oracle EBS DataGuard setup
2021-07-05c /cad secondary development create arc (4)
@Transational踩坑