You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中移除多维数组各层级的重复值

PHP多维数组递归去重

原始数组

Array
(
    [0] => Array
        (
            [id] => 1
            [text] => Vehicule
            [state] => Array
                (
                    [opened] => 1
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [text] => Cars
                            [parent] => 1
                            [children] => Array
                            (
                                [0] => Array
                                (
                                [id] => 99
                                [text] => BMW
                                [parent] => 2
                            )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [text] => Bikes
                            [parent] => 1
                        )

                    [2] => Array
                        (
                            [id] => 12
                            [text] => Boat
                            [parent] => 1
                        )

                    [3] => Array
                        (
                            [id] => 2
                            [text] => Cars
                            [parent] => 1
                            [children] => Array
                            (
                                [0] => Array
                                (
                                [id] => 99
                                [text] => BMW
                                [parent] => 2
                            )
                        )

                    [4] => Array
                        (
                            [id] => 3
                            [text] => Bikes
                            [parent] => 1
                        )

                    [5] => Array
                        (
                            [id] => 12
                            [text] => Boat
                            [parent] => 1
                        )

                )

        )

    [1] => Array
        (
            [id] => 1
            [text] => Vehicule
            [state] => Array
                (
                    [opened] => 1
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [text] => Cars
                            [parent] => 1
                            [children] => Array
                            (
                                [0] => Array
                                (
                                [id] => 99
                                [text] => BMW
                                [parent] => 2
                            )
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [text] => Bikes
                            [parent] => 1
                        )

                    [2] => Array
                        (
                            [id] => 12
                            [text] => Boat
                            [parent] => 1
                        )

                    [3] => Array
                        (
                            [id] => 2
                            [text] => Cars
                            [parent] => 1
                            [children] => Array
                            (
                                [0] => Array
                                (
                                [id] => 99
                                [text] => BMW
                                [parent] => 2
                            )
                        )

                    [4] => Array
                        (
                            [id] => 3
                            [text] => Bikes
                            [parent] => 1
                        )

                    [5] => Array
                        (
                            [id] => 12
                            [text] => Boat
                            [parent] => 1
                        )

                )

        )

)

期望去重结果

Array
(
    [0] => Array
        (
            [id] => 1
            [text] => Vehicule
            [state] => Array
                (
                    [opened] => 1
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [text] => Cars
                            [parent] => 1
                            [children] => Array
                            (
                                [0] => Array
                                (
                                [id] => 99
                                [text] => BMW
                                [parent] => 2
                            )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [text] => Bikes
                            [parent] => 1
                        )

                    [2] => Array
                        (
                            [id] => 12
                            [text] => Boat
                            [parent] => 1
                        )

                )

        )

)

尝试过的代码(仅处理顶层去重)

$arr = array_map("unserialize", array_unique(array_map("serialize", $arr)));

这段代码只能移除顶层的重复数组,无法深入到children这类子层级处理重复元素。

递归去重解决方案

要实现全层级的多维数组去重,需要编写递归函数,遍历数组的每一个元素:

  1. 对当前层级的数组用serialize+array_unique去重
  2. 遍历当前层级的每个元素,如果元素是数组,则递归调用去重函数处理子数组

代码实现:

function recursiveArrayUnique($array) {
    // 处理当前层级的去重
    $unique = array_map("unserialize", array_unique(array_map("serialize", $array)));
    
    // 递归处理子数组
    foreach ($unique as &$value) {
        if (is_array($value)) {
            $value = recursiveArrayUnique($value);
        }
    }
    
    return $unique;
}

// 使用示例
$arr = recursiveArrayUnique($arr);

代码说明

  • 递归函数recursiveArrayUnique先对当前层级的数组进行去重,逻辑和你之前的代码一致
  • 随后遍历每个元素,若元素是数组类型,就递归调用自身处理子层级的重复元素
  • 这样就能实现从顶层到所有子层级的全量去重,得到期望的结果

内容的提问来源于stack exchange,提问作者executable

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:35:27