按键在多维数组中取消设置

时间:2016-10-02 06:03:25

标签: php

我有这个数组,我想取消所有"下载"阵列

    Array
(
    [assetIndex] => Array
        (
            [id] => 1.10
            [sha1] => 72241db3c0bdc39e39b202182ff0000da5271a1d
            [size] => 145874
            [url] => https://launchermeta.mojang.com/mc/assets/1.10/72241db3c0bdc39e39b202182ff0000da5271a1d/1.10.json
            [totalSize] => 122198775
        )

    [assets] => 1.10
    [downloads] => Array
        (
            [client] => Array
                (
                    [sha1] => dc8e75ac7274ff6af462b0dcec43c307de668e40
                    [size] => 8857653
                    [url] => https://launcher.mojang.com/mc/game/1.10.2/client/dc8e75ac7274ff6af462b0dcec43c307de668e40/client.jar
                )

            [server] => Array
                (
                    [sha1] => 3d501b23df53c548254f5e3f66492d178a48db63
                    [size] => 9459897
                    [url] => https://launcher.mojang.com/mc/game/1.10.2/server/3d501b23df53c548254f5e3f66492d178a48db63/server.jar
                )

        )

    [id] => 1.10.2
    [libraries] => Array
        (
            [0] => Array
                (
                    [name] => com.mojang:netty:1.6
                    [downloads] => Array
                        (
                            [artifact] => Array
                                (
                                    [size] => 7877
                                    [sha1] => 4b75825a06139752bd800d9e29c5fd55b8b1b1e4
                                    [path] => com/mojang/netty/1.6/netty-1.6.jar
                                    [url] => https://libraries.minecraft.net/com/mojang/netty/1.6/netty-1.6.jar
                                )

                        )

                )

            [1] => Array
                (
                    [name] => oshi-project:oshi-core:1.1
                    [downloads] => Array
                        (
                            [artifact] => Array
                                (
                                    [size] => 30973
                                    [sha1] => 9ddf7b048a8d701be231c0f4f95fd986198fd2d8
                                    [path] => oshi-project/oshi-core/1.1/oshi-core-1.1.jar
                                    [url] => https://libraries.minecraft.net/oshi-project/oshi-core/1.1/oshi-core-1.1.jar
                                )

                        )

                )

            [2] => Array
                (
                    [name] => net.java.dev.jna:jna:3.4.0
                    [downloads] => Array
                        (
                            [artifact] => Array
                                (
                                    [size] => 1008730
                                    [sha1] => 803ff252fedbd395baffd43b37341dc4a150a554
                                    [path] => net/java/dev/jna/jna/3.4.0/jna-3.4.0.jar
                                    [url] => https://libraries.minecraft.net/net/java/dev/jna/jna/3.4.0/jna-3.4.0.jar
                                )

                        )

                )

            [3] => Array
                (
                    [name] => net.java.dev.jna:platform:3.4.0
                    [downloads] => Array
                        (
                            [artifact] => Array
                                (
                                    [size] => 913436
                                    [sha1] => e3f70017be8100d3d6923f50b3d2ee17714e9c13
                                    [path] => net/java/dev/jna/platform/3.4.0/platform-3.4.0.jar
                                    [url] => https://libraries.minecraft.net/net/java/dev/jna/platform/3.4.0/platform-3.4.0.jar
                                )

                        )

                )
        )
)

我正在使用search_array,我假设因为它是多维的,所以它找不到它。我怎样才能递归搜索? 我也尝试过一些我在网上找到的例子,但我找不到符合我需要的东西。

2 个答案:

答案 0 :(得分:1)

这个怎么样

function array_unset_recursive(&$array, $removeKey) {
    foreach ($array as $key => &$value) {
        if($key == $removeKey){
            unset($array[$key]);
        }
        else if (is_array($value)) {
            array_unset_recursive($value, $removeKey);
        }
    }
}

array_unset_recursive($arr, 'downloads');

答案 1 :(得分:0)

您只需为主unset索引运行downloads即可。对于子数组,只需循环遍历$array['libraries']并继续取消设置子数组downloads字段。

unset($array['downloads']); /* Unset the downloads field within the main array */

foreach ($array['libraries'] as &$arr) {
    unset($arr['downloads']); /* Unset the downloads field within the subarray */
}