一段php树状数组 如何简化

发布于 2022-08-31 20:33:38 字数 20881 浏览 19 评论 0

下面是一段非常复杂的树状数组
如何提取所有的[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
                                                                        )

                                                                )

                                                        )

                                                )

                                        )
                                )
                        )
                )
        )
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左耳近心 2022-09-07 20:33:38

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

<?php
    $data = [
        [
            'id' => 1,
            'pid' => 1,
            'name' => 'admin',
            'children' => [
                [
                    'id' => 158,
                    'pid' => 1,
                    'name' => '000c',
                    'children' => [
                        ['id' => 93, 'pid' => 72, 'name' => '000c11'],
                        ['id' => 96, 'pid' => 72, 'name' => '000c12'],
                    ]
                ]
            ]
        ]
    ];

    $result = array();
    array_walk_recursive($data, function($value, $key) use(&$result) {
        if ('name' == $key)
            $result[] = $value;
    });

    $result = array_reverse($result); // 翻转数组

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

$data = [
    [
        'id' => 1,
        'pid' => 1,
        'name' => 'admin',
        'children' => [
            [
                'id' => 158,
                'pid' => 1,
                'name' => '000c',
                'children' => [
                    ['id' => 93, 'pid' => 72, 'name' => '000c11'],
                    ['id' => 96, 'pid' => 72, 'name' => '000c12'],
                ]
            ]
        ]
    ]
];

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['name'], $value['id']]);

    if (isset($value['children'])) {
        foreach ($value['children'] 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";
}
草莓味的萝莉 2022-09-07 20:33:38

想说你这个数组真hentai....、

//$array 是你的原始数组
$arr = [];
array_walk_recursive($array, function($v, $k) use ($arr) {
    if($k === "name") $arr[] = $v;
});
$arr = array_reverse($arr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文