Home >Backend Development >PHP Tutorial >如何简化php树状数组

如何简化php树状数组

PHPz
PHPzOriginal
2016-06-06 20:38:421693browse

简化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中文网

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn