PHP 中的动态数组遍历

发布于 2024-09-03 00:53:49 字数 748 浏览 5 评论 0原文

我想从一维数组构建层次结构,并且(几乎)可以使用或多或少的硬编码代码来实现。如何使代码动态化?

也许使用 while(isset($array[$key])) { ... } ? 或者,有额外的功能?像这样: $out = my_extra_traverse_function($array,$key);

function array_traverse($array,$key=NULL) {
    $out = (string) $key;
    $out = $array[$key] . "/" . $out;

    $key = $array[$key];
    $out = $array[$key] ? $array[$key] . "/" . $out : "";
    $key = $array[$key];
    $out = $array[$key] ? $array[$key] . "/" . $out : "";
    $key = $array[$key];
    $out = $array[$key] ? $array[$key] . "/" . $out : "";

    return $out;
}

$a = Array(102=>101, 103=>102, 105=>107, 109=>105, 111=>109, 104=>111);
echo array_traverse($a,104);

输出:107/105/109/111/104

I want to build a hierarchy from a one-dimensional array and can (almost) do so with a more or less hardcoded code. How can I make the code dynamic?

Perhaps with while(isset($array[$key])) { ... }?
Or, with an extra function? Like this: $out = my_extra_traverse_function($array,$key);

function array_traverse($array,$key=NULL) {
    $out = (string) $key;
    $out = $array[$key] . "/" . $out;

    $key = $array[$key];
    $out = $array[$key] ? $array[$key] . "/" . $out : "";
    $key = $array[$key];
    $out = $array[$key] ? $array[$key] . "/" . $out : "";
    $key = $array[$key];
    $out = $array[$key] ? $array[$key] . "/" . $out : "";

    return $out;
}

$a = Array(102=>101, 103=>102, 105=>107, 109=>105, 111=>109, 104=>111);
echo array_traverse($a,104);

Output: 107/105/109/111/104

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

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

发布评论

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

评论(2

情深如许 2024-09-10 00:53:49

我认为你想要:

function array_traverse($array, $key = null) {
    $out = (string) $key;
    if (isset($array[$key])) {
        $out = array_traverse($array, $array[$key]) . '/' . $out;
    }
    return $out;
}

或者,对于非递归方法:

function array_traverse($array, $key = null) {
    $out = (string) $key;
    while(isset($array[$key])) {
        $out = $array[$key] . '/' . $out;
        $key = $array[$key];
    }
    return $out;
}

I THINK you want:

function array_traverse($array, $key = null) {
    $out = (string) $key;
    if (isset($array[$key])) {
        $out = array_traverse($array, $array[$key]) . '/' . $out;
    }
    return $out;
}

Or, for a non-recursive method:

function array_traverse($array, $key = null) {
    $out = (string) $key;
    while(isset($array[$key])) {
        $out = $array[$key] . '/' . $out;
        $key = $array[$key];
    }
    return $out;
}
诺曦 2024-09-10 00:53:49

是的,我想说 isset() 是正确的方法:

traverse($array, $value) {
    $result = array();
    while (isset($array[$value])) {
        array_unshift($result, $value);
        # or just $result[] = $value if you want to append
        # instead of prepending
        $value = $array[$value];
    }
    return $result;
    # or return implode('/', traverse(array(...))),
    # but I always prefer array return types in such cases:
    # they are much more flexible to the users of the function
}

# BTW: Using implode will avoid having an unnecessary
# leading/trailing delimiter (slash in this case)
echo implode('/', traverse(array(...)));

Yes, I would say isset() is the way to go:

traverse($array, $value) {
    $result = array();
    while (isset($array[$value])) {
        array_unshift($result, $value);
        # or just $result[] = $value if you want to append
        # instead of prepending
        $value = $array[$value];
    }
    return $result;
    # or return implode('/', traverse(array(...))),
    # but I always prefer array return types in such cases:
    # they are much more flexible to the users of the function
}

# BTW: Using implode will avoid having an unnecessary
# leading/trailing delimiter (slash in this case)
echo implode('/', traverse(array(...)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文