登录  /  注册

如何简化php树状数组

PHPz
发布: 2016-06-06 20:38:42
原创
1545人浏览过

简化php树状数组的方法:首先使用“array_walk_recursive”函数递归处理数组元素;然后使用“array_reverse”方法翻转得到一维数组即可达到简化php树状数组的效果。

如何简化php树状数组

如何简化php树状数组 ?

问题:

下面是一段非常复杂的树状数组

如何提取所有的[name]值, 按倒序简化保存到一个一维数组

Array
(
    [0] => Array
        (
            [id] => 1
            [pid] =>
            [name] => admin
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 58
                            [pid] => 1
                            [name] => 000c
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 72
                                            [pid] => 58
                                            [name] => 000c1
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 93
                                                            [pid] => 72
                                                            [name] => 000c11
                                                        )
                                                    [1] => Array
                                                        (
                                                            [id] => 96
                                                            [pid] => 72
                                                            [name] => 000c12
                                                        )
                                                )
                                        )
                                    [1] => Array
                                        (
                                            [id] => 81
                                            [pid] => 58
                                            [name] => 000c2
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 99
                                                            [pid] => 81
                                                            [name] => 000c21
                                                        )
                                                    [1] => Array
                                                        (
                                                            [id] => 101
                                                            [pid] => 81
                                                            [name] => 000C22
                                                        )
                                                )
                                        )
                                    [2] => Array
                                        (
                                            [id] => 87
                                            [pid] => 58
                                            [name] => 000c3
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 104
                                                            [pid] => 87
                                                            [name] => 000C31
                                                        )
                                                    [1] => Array
                                                        (
                                                            [id] => 108
                                                            [pid] => 87
                                                            [name] => 000C32
                                                        )
                                                )
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [id] => 44
                            [pid] => 1
                            [name] => ???
                        )
                    [2] => Array
                        (
                            [id] => 45
                            [pid] => 1
                            [name] => ??
                        )
                    [3] => Array
                        (
                            [id] => 46
                            [pid] => 1
                            [name] => ??
                        )
                    [4] => Array
                        (
                            [id] => 49
                            [pid] => 1
                            [name] => only
                        )
                    [5] => Array
                        (
                            [id] => 52
                            [pid] => 1
                            [name] => 007?
                        )
                    [6] => Array
                        (
                            [id] => 53
                            [pid] => 1
                            [name] => 0001e
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 54
                                            [pid] => 53
                                            [name] => 000e1
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 130
                                                            [pid] => 54
                                                            [name] => 000e11
                                                        )
                                                    [1] => Array
                                                        (
                                                            [id] => 131
                                                            [pid] => 54
                                                            [name] => 000e12
                                                        )
                                                )
                                        )
                                    [1] => Array
                                        (
                                            [id] => 73
                                            [pid] => 53
                                            [name] => 000e2
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 125
                                                            [pid] => 73
                                                            [name] => 000e21
                                                            [children] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [id] => 154
                                                                            [pid] => 125
                                                                            [name] => gfhgfhgf
                                                                        )
                                                                    [1] => Array
                                                                        (
                                                                            [id] => 155
                                                                            [pid] => 125
                                                                            [name] => hgfhgf
                                                                        )
                                                                    [2] => Array
                                                                        (
                                                                            [id] => 156
                                                                            [pid] => 125
                                                                            [name] => fghfgh
                                                                            [children] => Array
                                                                                (
                                                                                    [0] => Array
                                                                                        (
                                                                                            [id] => 157
                                                                                            [pid] => 156
                                                                                            [name] => fghfghf
                                                                                        )
                                                                                    [1] => Array
                                                                                        (
                                                                                            [id] => 158
                                                                                            [pid] => 156
                                                                                            [name] => hgfghff
                                                                                        )
                                                                                    [2] => Array
                                                                                        (
                                                                                            [id] => 159
                                                                                            [pid] => 156
                                                                                            [name] => rygfgfg
                                                                                        )
                                                                                    [3] => Array
                                                                                        (
                                                                                            [id] => 160
                                                                                            [pid] => 156
                                                                                            [name] => dgdfgd
                                                                                            [children] => Array
                                                                                                (
                                                                                                    [0] => Array
                                                                                                        (
                                                                                                            [id] => 161
                                                                                                            [pid] => 160
                                                                                                            [name] => ghgjhgjh
                                                                                                        )
                                                                                                    [1] => Array
                                                                                                        (
                                                                                                            [id] => 162
                                                                                                            [pid] => 160
                                                                                                            [name] => ghfhgfhgf
                                                                                                        )
                                                                                                    [2] => Array
                                                                                                        (
                                                                                                            [id] => 163
                                                                                                            [pid] => 160
                                                                                                            [name] => ghfhgfgh
                                                                                                        )
                                                                                                )
                                                                                        )
                                                                                )
                                                                        )
                                                                    [3] => Array
                                                                        (
                                                                            [id] => 164
                                                                            [pid] => 125
                                                                            [name] => 000e211
                                                                        )
                                                                )
                                                        )
                                                    [1] => Array
                                                        (
                                                            [id] => 126
                                                            [pid] => 73
                                                            [name] => 000e22
                                                            [children] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [id] => 136
                                                                            [pid] => 126
                                                                            [name] => rtyrtyrtyr
                                                                        )
                                                                    [1] => Array
                                                                        (
                                                                            [id] => 137
                                                                            [pid] => 126
                                                                            [name] => fghfghfhgf
                                                                        )
                                                                    [2] => Array
                                                                        (
                                                                            [id] => 138
                                                                            [pid] => 126
                                                                            [name] => fdgfdgfd
                                                                        )
                                                                    [3] => Array
                                                                        (
                                                                            [id] => 139
                                                                            [pid] => 126
                                                                            [name] => dfdgfd
                                                                        )
                                                                    [4] => Array
                                                                        (
                                                                            [id] => 140
                                                                            [pid] => 126
                                                                            [name] => fghfhgfg
                                                                        )
                                                                    [5] => Array
                                                                        (
                                                                            [id] => 141
                                                                            [pid] => 126
                                                                            [name] => hgfhgfhf
                                                                        )
                                                                    [6] => Array
                                                                        (
                                                                            [id] => 142
                                                                            [pid] => 126
                                                                            [name] => gfhfghfhgf
                                                                        )
                                                                    [7] => Array
                                                                        (
                                                                            [id] => 143
                                                                            [pid] => 126
                                                                            [name] => ghfghff
                                                                        )
                                                                    [8] => Array
                                                                        (
                                                                            [id] => 144
                                                                            [pid] => 126
                                                                            [name] => fghfhgf
                                                                        )
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
)
登录后复制

方法:

使用array_walk_recursive 递归处理数组元素, 并使用array_reverse翻转得到一维数组:

<?php
    $data = [
        [
            &#39;id&#39; => 1,
            &#39;pid&#39; => 1,
            &#39;name&#39; => &#39;admin&#39;,
            &#39;children&#39; => [
                [
                    &#39;id&#39; => 158,
                    &#39;pid&#39; => 1,
                    &#39;name&#39; => &#39;000c&#39;,
                    &#39;children&#39; => [
                        [&#39;id&#39; => 93, &#39;pid&#39; => 72, &#39;name&#39; => &#39;000c11&#39;],
                        [&#39;id&#39; => 96, &#39;pid&#39; => 72, &#39;name&#39; => &#39;000c12&#39;],
                    ]
                ]
            ]
        ]
    ];
    $result = array();
    array_walk_recursive($data, function($value, $key) use(&$result) {
        if (&#39;name&#39; == $key)
            $result[] = $value;
    });
    $result = array_reverse($result); // 翻转数组
登录后复制

下面拓展一个以id排序的方法, 仅供参考:

$data = [
    [
        &#39;id&#39; => 1,
        &#39;pid&#39; => 1,
        &#39;name&#39; => &#39;admin&#39;,
        &#39;children&#39; => [
            [
                &#39;id&#39; => 158,
                &#39;pid&#39; => 1,
                &#39;name&#39; => &#39;000c&#39;,
                &#39;children&#39; => [
                    [&#39;id&#39; => 93, &#39;pid&#39; => 72, &#39;name&#39; => &#39;000c11&#39;],
                    [&#39;id&#39; => 96, &#39;pid&#39; => 72, &#39;name&#39; => &#39;000c12&#39;],
                ]
            ]
        ]
    ]
];
class MaxHeap extends SplMaxHeap {
    public function compare($array1, $array2)
    {
        if ($array1[1] === $array2[1]) return 0;
        return $array1[1] < $array2[1] ? -1 : 1;
    }
}
function walk_sort($maxHeap, $value) {
    $maxHeap->insert([$value[&#39;name&#39;], $value[&#39;id&#39;]]);
    if (isset($value[&#39;children&#39;])) {
        foreach ($value[&#39;children&#39;] as $children) {
            walk_sort($maxHeap, $children);
        }
    }
}
$maxHeap = new MaxHeap();
foreach ($data as $value) {
    walk_sort($maxHeap, $value);
}
foreach ($maxHeap as $value) {
    echo "- ", $value[0], "\n";
}
登录后复制

更多相关技术知识,请访问PHP中文网

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号