moustiluigi moustiluigi - 2 months ago 13
PHP Question

Unset in multidimentionnal array by key

I have this array and I would like to unset all the "downloads" arrays

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
)

)

)
)
)


I am using search_array, and I am assuming that because it is multidimensional it isn't finding it. How can I search recursively?
I also tried a bunch of examples I found online but I can't find anything that suits my needs.

Answer

what about this

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');