当前位置:网站首页>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" => "黄鹤楼",
],
];
边栏推荐
- Win10: add or delete boot items, and add user-defined boot files to boot items
- js的防抖和节流
- MySQL index
- apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
- Improve user experience defensive programming
- Cve-2015-1635 (ms15-034) Remote Code Execution Vulnerability recurrence
- [literature reading and thought notes 13] unprocessing images for learned raw denoising
- js删除字符串的最后一个字符
- Tool grass welfare post
- Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
猜你喜欢

Blog directory of zzq -- updated on 20210601

Sqli labs customs clearance summary-page4

SQLI-LABS通关(less1)

SQLI-LABS通关(less18-less20)

Cve - 2015 - 1635 (ms15 - 034) réplication de la vulnérabilité d'exécution de code à distance

unittest. Texttestrunner does not generate TXT test reports

Latex compiles Chinese in vscode and solves the problem of using Chinese path

The win10 network icon disappears, and the network icon turns gray. Open the network and set the flash back to solve the problem

sqli-labs通关汇总-page2

js中map和forEach的用法
随机推荐
Flask migrate cannot detect db String() equal length change
PIP install
Nodejs - Express middleware modification header: typeerror [err_invalid_char]: invalid character in header content
unittest. Texttestrunner does not generate TXT test reports
Kali latest update Guide
CAD二次开发 对象
部署api_automation_test过程中遇到的问题
查询GPU时无进程运行,但是显存却被占用了
[daily question] - Huawei machine test 01
默认google浏览器打不开链接(点击超链接没有反应)
Stress test modification solution
Uniapp introduces local fonts
QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)
Solve the problem of bindchange event jitter of swiper component of wechat applet
JS to determine whether there is a value in the object in the array
In depth study of JVM bottom layer (II): hotspot virtual machine object
Pytest (1) case collection rules
Fe - weex uses a simple encapsulated data loading plug-in as the global loading method
SQLI-LABS通关(less1)
Fe - use of weex development weex UI components and configuration use