当前位置:网站首页>php中树形结构转数组(拉平树结构,保留上下级排序)
php中树形结构转数组(拉平树结构,保留上下级排序)
2022-07-02 06:23:00 【夜空の雪風】
前言
在做php中树形结构转数组的功能中有时候需要把树结构直接拉平按照树结构的顺序按照由上到下排列,这时候就需要另外一种处理方法了。
解决办法
//树结构数据转数组(拉平树结构,保留上下级排序)
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;
}
直接拉平树状结构,保留上下排序。
$tree = [
[
"key" => 1,
"p_key" => 0,
"title" => "湖北省",
"children" => [
[
"key" => 2,
"p_key" => 1,
"title" => "武汉市",
"children" => [
[
"key" => 3,
"p_key" => 2,
"title" => "武昌区",
"children" => [
[
"key" => 4,
"p_key" => 3,
"title" => "蛇山西山坡特1号",
"children" => [
[
"key" => 5,
"p_key" => 4,
"title" => "黄鹤楼",
],
],
],
],
],
],
],
],
],
];
$data = [
[
"key" => 1,
"p_key" => 0,
"title" => "湖北省",
],
[
"key" => 2,
"p_key" => 1,
"title" => "武汉市",
],
[
"key" => 3,
"p_key" => 2,
"title" => "武昌区",
],
[
"key" => 4,
"p_key" => 3,
"title" => "蛇山西山坡特1号",
],
[
"key" => 5,
"p_key" => 4,
"title" => "黄鹤楼",
],
];
边栏推荐
- Fe - wechat applet - Bluetooth ble development research and use
- Solve the problem of bindchange event jitter of swiper component of wechat applet
- Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
- 工具种草福利帖
- pm2简单使用和守护进程
- Selenium+msedgedriver+edge browser installation driver pit
- CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
- Unexpected inconsistency caused by abnormal power failure; Run fsck manually problem resolved
- The win10 network icon disappears, and the network icon turns gray. Open the network and set the flash back to solve the problem
- Differences between ts and JS
猜你喜欢
SQLI-LABS通关(less6-less14)
Sqli - Labs Clearance (less6 - less14)
Sqli-labs customs clearance (less6-less14)
The table component specifies the concatenation parallel method
SQL injection closure judgment
Build learning tensorflow
Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决
sqli-labs通关汇总-page2
CVE-2015-1635(MS15-034 )遠程代碼執行漏洞複現
SQLI-LABS通关(less1)
随机推荐
sqli-labs通关汇总-page4
No process runs when querying GPU, but the video memory is occupied
Wechat applet Foundation
PXC high availability cluster summary
Latex warning: citation "*****" on page y undefined on input line*
2021-07-05C#/CAD二次开发创建圆弧(4)
SQLI-LABS通关(less1)
JS divides an array into groups of three
PHP Session原理简析
Huawei mindspire open source internship machine test questions
Anti shake and throttling of JS
解决微信小程序swiper组件bindchange事件抖动问题
Self study table Au
Flask migrate cannot detect db String() equal length change
SQLI-LABS通关(less18-less20)
Flex Jiugongge layout
CAD二次开发 对象
CTF three count
MySQL index
2021-07-19C#CAD二次开发创建多线段