多维嵌套数组,自定义字段指定条件下执行自定义处理
function recursiveCallback(array &$array, callable $callable_condition, callable $callable, $field)
{
foreach ($array as $k => $value) {
if ($k == $field && eval('return '.call_user_func($callable_condition, $value).';')) {
$array = call_user_func($callable, $array, $k);
}
if (isset($array[$k]) && is_array($array[$k])) {
$array[$k] = recursiveCallback($array[$k], $callable_condition, $callable, $field);
}
}
return $array;
}
调用方法
recursiveCallback($array, function ($value) {
return is_array($value) && !$value;
}, function ($v, $key) {
unset($v[$key]);
return $v;
}, 'children');