gpt4 book ai didi

php - 仅通过检查键来递归删除数组及其子元素的元素

转载 作者:行者123 更新时间:2023-12-01 23:15:15 25 4
gpt4 key购买 nike

我正在寻找一种通过仅检查键来递归删除数组的部分以及这些部分的子项的方法。

在下面的示例中,$array 是输入数组,$remove 包含应从 $array 中删除的键:

$array = [
'key1' => [
'key11' => [],
'key12' => [
'key121' => [],
'key122' => [],
'key123' => [],
],
'key13' => [],
],
'key2' => [
'key21' => [],
'key22' => [],
'key23' => [],
'key24' => [],
'key25' => [
'key251' => [
'key2511' => [],
'key2512' => [],
'key2513' => [],
'key2514' => [],
'key2515' => [],
],
'key252' => [
'key2521' => [],
'key2522' => [],
'key2523' => [],
'key2524' => [],
'key2525' => [],
],

],
],
'key3' => [
'key31' => [],
'key32' => [],
'key33' => [],
'key34' => [],
'key35' => [
'key351' => [
'key3511' => [],
'key3512' => [],
'key3513' => [],
'key3514' => [],
'key3515' => [],

],
],
],
];

$remove = [
'key1' => [
'key12' => [
'key121' => [],
],
'key13' => [],
],
'key2' => [
'key25' => [
'key251' => [
'key2514' => [
],
],
'key252' => [],
],
],
'key3' => [],
];

我写了一个丑陋的非递归算法:

foreach ($array as $k1 => $v1) {
foreach ($v1 as $k2 => $v2) {
foreach ($v2 as $k3 => $v3) {
foreach ($v3 as $k4 => $v4) {
if (isset($array[$k1][$k2][$k3][$k4]) && isset($remove[$k1][$k2][$k3][$k4]) && 0 === count($remove[$k1][$k2][$k3][$k4])) {
unset($array[$k1][$k2][$k3][$k4]);
}
}
if (isset($array[$k1][$k2][$k3]) && isset($remove[$k1][$k2][$k3]) && 0 === count($remove[$k1][$k2][$k3])) {
unset($array[$k1][$k2][$k3]);
}
}
if (isset($array[$k1][$k2]) && isset($remove[$k1][$k2]) && 0 === count($remove[$k1][$k2])) {
unset($array[$k1][$k2]);
}
}
if (isset($array[$k1]) && isset($remove[$k1]) && 0 === count($remove[$k1])) {
unset($array[$k1]);
}
}

var_dump($array);

它返回我正在寻找的输出:

array(2) {
["key1"]=>
array(2) {
["key11"]=>
array(0) {
}
["key12"]=>
array(2) {
["key122"]=>
array(0) {
}
["key123"]=>
array(0) {
}
}
}
["key2"]=>
array(5) {
["key21"]=>
array(0) {
}
["key22"]=>
array(0) {
}
["key23"]=>
array(0) {
}
["key24"]=>
array(0) {
}
["key25"]=>
array(1) {
["key251"]=>
array(4) {
["key2511"]=>
array(0) {
}
["key2512"]=>
array(0) {
}
["key2513"]=>
array(0) {
}
["key2515"]=>
array(0) {
}
}
}
}
}

我的问题是,如何使此功能递归,因为理论上 $array 可以无限期嵌套?

任何帮助将不胜感激。谢谢!

<小时/>

与此同时,我想出了一个替代解决方案:

function array_remove_key_recursive($input, $remove)
{
$ret = [];

foreach ($input as $key => $value) {
if (array_key_exists($key, $remove)) {
if (is_array($value)) {
if (count($remove[$key]) > 0) {
$diff_recursive = array_remove_key_recursive($value, $remove[$key]);
if (count($diff_recursive) > 0) {
$ret[$key] = $diff_recursive;
}
}
} else {
if ($value != $remove[$key]) {
$ret[$key] = $value;
}
}
} else {
$ret[$key] = $value;
}
}

return $ret;
}

最佳答案

这是我的做法

function test (&$arr, $remove) {
$keys =array_keys ($remove, [], true) ;
$arr =array_filter ($arr, function ($v, $k) use ($remove, $keys) {
return !in_array ($k, $keys, true) ;
},
ARRAY_FILTER_USE_BOTH
) ;
$keys =array_diff (array_keys ($remove), $keys) ;
foreach ( $keys as $key )
test ($arr [$key], $remove [$key]) ;
}

test ($array, $remove) ;
print_r ($array) ;

关于php - 仅通过检查键来递归删除数组及其子元素的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34740760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com