使用另一个数组搜索多维数组的键

发布于 2024-12-17 21:37:15 字数 709 浏览 0 评论 0原文

是否有一种优雅的方法可以使用另一个数组作为要查找的键从大量多维数组中获取值?

例如,

$cats[A][A1][A11][A111] = $val;
$cats[A][A1][A11][A112] = $val;
$cats[A][A1][A12] = $val;
$cats[A][A1][A12][A121] = $val;
$cats[A][A2] = $val;
$cats[A][A2][A21] = $val;
$cats[A][A2][A22] = $val;
$cats[A][A2][A22][A221] = $val;
$cats[A][A2][A22][A222] = $val;

使用 $keys = Array ('A', 'A2', 'A22', 'A221'); 访问 $cats 中的值,

而不检查 $keys 并做类似的事情...

switch (count($keys)) {
   case 1: $val = $cats[$keys[0]]; break;
   case 2: $val = $cats[$key[0]][$key[1]]; break;
   case 3: $val = $cats[$key[0]][$key[1]][$key[2]]; break;
   ...
}

非常感谢。

Is there an elegant way of getting values from a massive multi-dimensional array using another array for the keys to lookup?

e.g.

$cats[A][A1][A11][A111] = $val;
$cats[A][A1][A11][A112] = $val;
$cats[A][A1][A12] = $val;
$cats[A][A1][A12][A121] = $val;
$cats[A][A2] = $val;
$cats[A][A2][A21] = $val;
$cats[A][A2][A22] = $val;
$cats[A][A2][A22][A221] = $val;
$cats[A][A2][A22][A222] = $val;

access values from $cats using $keys = Array ('A', 'A2', 'A22', 'A221');

without checking the length of $keys and doing something like...

switch (count($keys)) {
   case 1: $val = $cats[$keys[0]]; break;
   case 2: $val = $cats[$key[0]][$key[1]]; break;
   case 3: $val = $cats[$key[0]][$key[1]][$key[2]]; break;
   ...
}

many thanks.

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

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

发布评论

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

评论(3

小女人ら 2024-12-24 21:37:15

为什么不使用递归呢?像这样的事情:

function get_val($array, $keys) {
    if(empty($keys) || !is_array($keys) || !is_array($array)) return $array;
    else {
        $first_key = array_shift($keys);
        return get_val($array[$first_key], $keys);
    }
}

我最初是在循环中编写的,但由于某种原因将其更改为递归。正如 yeoman 所说,递归函数比循环更有可能导致堆栈溢出,特别是如果您的数组足够大(PHP 确实支持结束递归),因此这里有一个应该实现相同目的的循环:

// given a multidimensional array $array and single-dimensional array of keys $keys
$desired_value = $array;
while(count($keys) > 0) {
    $first_key = array_shift($keys);
    $desired_value = $desired_value[$first_key];
}

Why not use recursion? Something like this:

function get_val($array, $keys) {
    if(empty($keys) || !is_array($keys) || !is_array($array)) return $array;
    else {
        $first_key = array_shift($keys);
        return get_val($array[$first_key], $keys);
    }
}

I originally had this written in a loop, but changed it to recursive for some reason. It's true, as yeoman said, that a recursive function is more likely than a loop to cause a stack overflow, especially if your array is sufficiently large (PHP does support end recursion), so here's a loop that should accomplish the same purpose:

// given a multidimensional array $array and single-dimensional array of keys $keys
$desired_value = $array;
while(count($keys) > 0) {
    $first_key = array_shift($keys);
    $desired_value = $desired_value[$first_key];
}
北方。的韩爷 2024-12-24 21:37:15

到目前为止还好。否则,您将需要迭代数组并检查深度。为了使其动态化,我确信您在构造 $cats 时将键添加到 $keys 数组中。使用递归也解决方案,它会花费更多的步骤,更多的内存。

That's fine so far. Otherwise you would need to iterate through array and check deepness. To make it dynamic I am sure you add keys into $keys array when constructing $cats. Using recursion also solution it will take more steps, more memory.

月亮坠入山谷 2024-12-24 21:37:15

jburbage建议使用递归原则上是可以的,但据我所知,PHP不支持结束递归。

问题是关于“大量”多维数组。

由于“大规模”除了总体尺寸大之外还意味着深度大,因此使用此解决方案可能会遇到堆栈溢出,因为通常可以在堆上创建比堆栈通过递归可以处理的更深的数据结构。

在这种情况下,从性能的角度来看,该方法也是不可取的。

只需重构 jburbage 的递归解决方案以在循环中工作,您就几乎完成了:-)

这是 jburbage 的原始建议代码:

function get_val($array, $keys) {
    if(empty($keys) || !is_array($keys) || !is_array($array)) return $array;
    else {
        $first_key = array_shift($keys);
        return get_val($array[$first_key], $keys);
    }
}

jburbage's suggestion of using recursion is OK in principle, but from what I know, PHP doesn't support end-recursion.

And the question was about a "massive" multidimensional array.

As "massive" suggests great depth in addition to great overall size, it's possible to run into a stack overflow with this solution, as it's usually possible to create data structures on the heap that reach deeper than the stack can cope with via recursion.

The approach is also not desirable from the performance point of view in this case.

Simply refactor jburbage's recursive solution to work in a loop, and you're almost there :-)

Here's jburbage's original suggested code once again:

function get_val($array, $keys) {
    if(empty($keys) || !is_array($keys) || !is_array($array)) return $array;
    else {
        $first_key = array_shift($keys);
        return get_val($array[$first_key], $keys);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文