PHP:递归数组函数

发布于 2024-09-03 13:11:44 字数 1543 浏览 3 评论 0原文

我想创建一个函数,返回从设置节点到根值的完整路径。我尝试创建一个递归函数,但完全运气不佳。执行此操作的适当方法是什么?我认为递归函数是唯一的方法?

这是数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Root category
            [_parent] => 
        )

    [1] => Array
        (
            [id] => 2
            [name] => Category 2
            [_parent] => 1
        )

    [2] => Array
        (
            [id] => 3
            [name] => Category 3
            [_parent] => 1
        )

    [3] => Array
        (
            [id] => 4
            [name] => Category 4
            [_parent] => 3
        )
)

当获取节点 id#4 的完整路径时,我希望函数输出的结果:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Root category
            [_parent] => 
        )

    [1] => Array
        (
            [id] => 3
            [name] => Category 3
            [_parent] => 1
        )

    [2] => Array
        (
            [id] => 4
            [name] => Category 4
            [_parent] => 3
        )
)

我的递归技能的臭名昭著的糟糕示例:

    function recursive ($id, $array) {

        $innerarray = array();
        foreach ($array as $k => $v) {

            if ($v['id'] === $id) {
                if ($v['_parent'] !== '') {
                    $innerarray[] = $v;
                    recursive($v['id'], $array);
                }
            }

        }
        return $innerarray; 
    }

I want to create a function that returns the full path from a set node, back to the root value. I tried to make a recursive function, but ran out of luck totally. What would be an appropriate way to do this? I assume that a recursive function is the only way?

Here's the array:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Root category
            [_parent] => 
        )

    [1] => Array
        (
            [id] => 2
            [name] => Category 2
            [_parent] => 1
        )

    [2] => Array
        (
            [id] => 3
            [name] => Category 3
            [_parent] => 1
        )

    [3] => Array
        (
            [id] => 4
            [name] => Category 4
            [_parent] => 3
        )
)

The result I want my function to output when getting full path of node id#4:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Root category
            [_parent] => 
        )

    [1] => Array
        (
            [id] => 3
            [name] => Category 3
            [_parent] => 1
        )

    [2] => Array
        (
            [id] => 4
            [name] => Category 4
            [_parent] => 3
        )
)

The notoriously bad example of my recursive skills:

    function recursive ($id, $array) {

        $innerarray = array();
        foreach ($array as $k => $v) {

            if ($v['id'] === $id) {
                if ($v['_parent'] !== '') {
                    $innerarray[] = $v;
                    recursive($v['id'], $array);
                }
            }

        }
        return $innerarray; 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

国产ˉ祖宗 2024-09-10 13:11:44

假设子数组中的“id”是父数组内的子数组索引+1(否则您需要每次都在数组中进行搜索),您可以这样做:

$searchNode = 4;
while ($searchNode)
{
    $result[] = $nodes[$searchNode - 1];
    $searchNode = $nodes[$searchNode - 1]["id"];
}
$result = array_reverse($result);

assuming "id" in your sub array is that sub arrays index + 1 inside the parent array (otherwise you would need to do a search in the array each time), you could do this:

$searchNode = 4;
while ($searchNode)
{
    $result[] = $nodes[$searchNode - 1];
    $searchNode = $nodes[$searchNode - 1]["id"];
}
$result = array_reverse($result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文