当前位置:网站首页>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" => "黄鹤楼",
],
];
边栏推荐
- There are multiple good constructors and room will problem
- The use of regular expressions in JS
- SQLI-LABS通關(less6-less14)
- Linux MySQL 5.6.51 Community Generic 安装教程
- Brief analysis of PHP session principle
- Differences between ts and JS
- Win10: add or delete boot items, and add user-defined boot files to boot items
- 蚂蚁集团g6初探
- Sqli labs customs clearance summary-page3
- Cve - 2015 - 1635 (ms15 - 034) réplication de la vulnérabilité d'exécution de code à distance
猜你喜欢
解决微信小程序swiper组件bindchange事件抖动问题
The use of regular expressions in JS
Solve the problem of bindchange event jitter of swiper component of wechat applet
Huawei mindspire open source internship machine test questions
Win电脑截图黑屏解决办法
Sentry construction and use
CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
In depth study of JVM bottom layer (IV): class file structure
js中map和forEach的用法
Latex compiles Chinese in vscode and solves the problem of using Chinese path
随机推荐
Atcoder beginer contest 253 F - operations on a matrix / / tree array
SQLI-LABS通关(less15-less17)
CTF three count
Utilisation de la carte et de foreach dans JS
Anti shake and throttling of JS
Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
PgSQL learning notes
JS modification element attribute flipping commonly used in selenium's Web Automation
Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
查询GPU时无进程运行,但是显存却被占用了
2021-07-17C#/CAD二次开发创建圆(5)
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
Fe - use of weex development weex UI components and configuration use
Usage of map and foreach in JS
MySQL中的正则表达式
sprintf_ How to use s
Pytest (1) case collection rules
Sentry搭建和使用