本文介绍了取消设置里面array_walk_recursive不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
array_walk_recursive($arr, function(&$val, $key){
if($val == 'smth'){
unset($val); // <- not working, unset($key) doesn't either
$var = null; // <- setting it to null works
}
});
print_r($arr);
我不希望它是空的,我想出来的完全数组的元素。这甚至可能与array_walk_recursive?
I don't want it to be null, I want the element out of the array completely. Is this even possible with array_walk_recursive?
推荐答案
您不能使用 array_walk_recursive
在这里,但你可以编写自己的函数。这很容易:
You can't use array_walk_recursive
here but you can write your own function. It's easy:
function array_unset_recursive(&$array, $remove) {
if (!is_array($remove)) $remove = array($remove);
foreach ($array as $key => &$value) {
if (in_array($value, $remove)) unset($array[$key]);
else if (is_array($value)) {
array_unset_recursive($value, $remove);
}
}
}
和用法:
array_unset_recursive($arr, 'smth');
或删除多个值:
array_unset_recursive($arr, array('smth', 51));
这篇关于取消设置里面array_walk_recursive不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!