使用另一个数组搜索多维数组的键
是否有一种优雅的方法可以使用另一个数组作为要查找的键从大量多维数组中获取值?
例如,
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用递归呢?像这样的事情:
我最初是在循环中编写的,但由于某种原因将其更改为递归。正如 yeoman 所说,递归函数比循环更有可能导致堆栈溢出,特别是如果您的数组足够大(PHP 确实支持结束递归),因此这里有一个应该实现相同目的的循环:
Why not use recursion? Something like this:
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:
到目前为止还好。否则,您将需要迭代数组并检查深度。为了使其动态化,我确信您在构造 $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.
jburbage建议使用递归原则上是可以的,但据我所知,PHP不支持结束递归。
问题是关于“大量”多维数组。
由于“大规模”除了总体尺寸大之外还意味着深度大,因此使用此解决方案可能会遇到堆栈溢出,因为通常可以在堆上创建比堆栈通过递归可以处理的更深的数据结构。
在这种情况下,从性能的角度来看,该方法也是不可取的。
只需重构 jburbage 的递归解决方案以在循环中工作,您就几乎完成了:-)
这是 jburbage 的原始建议代码:
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: