PHP 有趣的递归与元素删除

发布于 2024-10-17 19:06:51 字数 2138 浏览 0 评论 0原文


我有一个多维数组。并且某些数组的元素具有“非活动”标志。我需要删除这个元素。但下线元素应该往上走。我写了一个函数,但它运行错误。仅当第一个数组元素处于非活动状态时它才能正常工作。
功能:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2024-10-24 19:06:51

这是一种赖特方法:

public function deleteInactive($children, $g = 0)
{
    $generation++;
    $copy = $children;
    if (!empty($copy) && is_array($copy)) {
        foreach ($copy as $k => $v) {
        $inactive = false;
        if (array_key_exists('inactive', $v) && $v['inactive']) {
            $children += $this->deleteInactive($v['children'], ($generation - 1));
            unset ($children[$k]);
            $inactive = true;
        } else {
            $children[$k] = $v;
            $children[$k]['children'] = $this->deleteInactive($v['children'], $generation);
        }
        if (!$inactive) {
            $children[$k]['generation'] = $generation;
        }
        }
    }
    return $children;
}

It is a wright method:

public function deleteInactive($children, $g = 0)
{
    $generation++;
    $copy = $children;
    if (!empty($copy) && is_array($copy)) {
        foreach ($copy as $k => $v) {
        $inactive = false;
        if (array_key_exists('inactive', $v) && $v['inactive']) {
            $children += $this->deleteInactive($v['children'], ($generation - 1));
            unset ($children[$k]);
            $inactive = true;
        } else {
            $children[$k] = $v;
            $children[$k]['children'] = $this->deleteInactive($v['children'], $generation);
        }
        if (!$inactive) {
            $children[$k]['generation'] = $generation;
        }
        }
    }
    return $children;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文