PHP 有趣的递归与元素删除
我有一个多维数组。并且某些数组的元素具有“非活动”标志。我需要删除这个元素。但下线元素应该往上走。我写了一个函数,但它运行错误。仅当第一个数组元素处于非活动状态时它才能正常工作。
功能:
function deleteInactive($children, $generation = 0)
{
$generation++;
$copy = $children;
if (!empty($copy) && is_array($copy)) {
foreach ($copy as $key => $v) {
$inactive = false;
if (array_key_exists('inactive', $v) && $v['inactive'] === true) {
$nextGeneration = $generation - 1;
$inactive = true;
$children = deleteInactive($v['children'], $nextGeneration);
unset($children[$key]);
} else {
$nextGeneration = $generation;
if (!empty($v['children']) && is_array($v['children'])) {
$children[$key] = $v;
$children[$key]['children'] = deleteInactive($v['children'], $generation);
}
}
if (!$inactive) {
$children[$key]['generation'] = $nextGeneration;
}
}
}
return $children;
}
测试数组:
$tree = array(
'id' => 1000,
'generation' => 0,
'children' => array(
1002 => array(
'id' => 1002,
'generation' => 1,
'children' => array(
1005 => array(
'id' => 1005,
'generation' => 2,
'inactive' => true,
'children' => array()
)
)
),
1006 => array(
'id' => 1006,
'generation' => 1,
'inactive' => true,
'children' => array(
1007 => array(
'id' => 1007,
'generation' => 2,
'children' => array()
)
)
),
1008 => array(
'id' => 1008,
'generation' => 1,
'children' => array(
1009 => array(
'id' => 1009,
'generation' => 2,
'children' => array()
)
)
)
)
);
测试:
print_r($tree);
$tree['children'] = deleteInactive($tree['children']);
print_r($tree);
I have a multidimensional array. And some of array's elements have 'inactive' flag. I need to remove this elements. But downline elements should be go up. I wrote a function, but it works wrong. It works fine only if first array element is inactive.
Function:
function deleteInactive($children, $generation = 0)
{
$generation++;
$copy = $children;
if (!empty($copy) && is_array($copy)) {
foreach ($copy as $key => $v) {
$inactive = false;
if (array_key_exists('inactive', $v) && $v['inactive'] === true) {
$nextGeneration = $generation - 1;
$inactive = true;
$children = deleteInactive($v['children'], $nextGeneration);
unset($children[$key]);
} else {
$nextGeneration = $generation;
if (!empty($v['children']) && is_array($v['children'])) {
$children[$key] = $v;
$children[$key]['children'] = deleteInactive($v['children'], $generation);
}
}
if (!$inactive) {
$children[$key]['generation'] = $nextGeneration;
}
}
}
return $children;
}
Test array:
$tree = array(
'id' => 1000,
'generation' => 0,
'children' => array(
1002 => array(
'id' => 1002,
'generation' => 1,
'children' => array(
1005 => array(
'id' => 1005,
'generation' => 2,
'inactive' => true,
'children' => array()
)
)
),
1006 => array(
'id' => 1006,
'generation' => 1,
'inactive' => true,
'children' => array(
1007 => array(
'id' => 1007,
'generation' => 2,
'children' => array()
)
)
),
1008 => array(
'id' => 1008,
'generation' => 1,
'children' => array(
1009 => array(
'id' => 1009,
'generation' => 2,
'children' => array()
)
)
)
)
);
Testing:
print_r($tree);
$tree['children'] = deleteInactive($tree['children']);
print_r($tree);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种赖特方法:
It is a wright method: