PHP的递归阵列搜索 - 返回特定父 [英] php recursive array search - return specific parent

查看:99
本文介绍了PHP的递归阵列搜索 - 返回特定父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,可以在其尺寸不同,有时可以是小的,有时可以更深。我试图寻找数组中的某个元素,如果被发现,我想获得一个特定的父母。因此,举例来说,如果我有一个数组为:

I have an array that can vary in its dimension, sometimes can be small, sometimes can go deeper. I'm trying to search for a particular element in the array and if it is found, I would like to get a specific parent. So for instance, if I have an array as:

再次尺寸可以改变,但是,我要寻找的最外父的有兄弟姐妹。 在这种情况下的)

Again, the dimension can change, however, I'm looking for the the most outer parent's key that has siblings. (in this case)

Array (
  [0] => Array (
    [0] => Array (              
        [0] => Array (         <<----------------------------------------+
              [0] => FOO0                                                |  
              [1] => BAR0         //Search BAR0 returns parent key 0     +
              [2] => Array(                                              |
                    [0] => HELLO0                                        |
                    [1] => BYE0   //Search BYE0 returns parent key 0     +
                  )                                                      |
              [3] => FOO0                                                |
              [4] => Array (                                             |
                    [0] => HELLO0  //Search HELLO0 returns parent key 0 --
                  )
            )
        [1] => Array (         <<----------------------------------------+
              [0] => FOO1                                                |
              [1] => BAR1                                                |
              [2] => Array (                                             |
                    [0] => HELLO1                                        |
                    [1] => BYE1                                          |
                  )                                                      |
              [3] => BASE1                                               |
              [4] => Array (                                             |
                    [0] => BAZ1                                          |
                    [1] => Array (                                       | 
                          [0] => DOO1 //Search DOO1 returns parent key 1 +
                          [1] => BOB2 //Search BOB2 returns parent key 1 +
                        )
                )
          )
        [2] => FOO2 // Search FOO2 returns key 2 (itself)
        )
    )
)

有关foo2的示例输出

Sample Output for FOO2

[2] => FOO2 // searched value

我真的AP preciate一些帮助!谢谢!

I would really appreciate some help! Thanks!

推荐答案

我不能完全肯定这是你在找什么,但你可以尝试一下:

I'm not completely sure this is what you are looking for, but you can give it a try:

function find($needle, array $haystack)
{
  foreach ($haystack as $i => $x) {
    if (is_array($x)) {
      $b = find($needle, $x);
      if ($b) return count($haystack) > 1 ? array($i, $x) : $b;
    }
    else if ($x == $needle) {
      return array($i, $x);
    }
  }

  return false;
}

list($key, $val) = find('FOO1', $data);

这不会返回准确的元素,但它的一个副本。如果你想在原来的项目,它需要更新为使用引用。

This doesn't return the exact element, but a copy of it. If you want the original item, it will need to be updated to use references.

您可以阵列($ I,$ X)更改为阵列($ I =&GT; $ x)在这两个地方,如果你不想使用列表查询功能时施工。但我觉得它更容易使用,因为它被写入工作。

You can change array($i, $x) to array($i => $x) in both places if you don't want to use the list construct when querying the function. But I think it's easier to work with as it is written.

这篇关于PHP的递归阵列搜索 - 返回特定父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆