检查是否数组多维数组中存在 - 无循环 - 深度未知 [英] Checking if array exists within multidimensional array - no looping - unknown depth

查看:130
本文介绍了检查是否数组多维数组中存在 - 无循环 - 深度未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做快速查找找到,如果在一个数组中存在数组。如果我知道该数组的深度这将是容易! - 和快速

I need to do fast lookups to find if an array exists in an array. If I knew the depth of the array It would be easy - and fast!

$heystack['lev1']['lev2']['lev3'] = 10; // $heystack stores 10,000s of arrays like this

if(isset($heystack[$var1][$var2][$var3])) do something...

你怎么会做这个动态,如果你不知道有多深?循环和每一级搜索将是我的应用程序太慢了。

How would you do this dynamically if you don't know the depth? looping and searching at each level will be too slow for my application.

推荐答案

您问题已经回答:

if (isset($heystack[$var1][$var2][$var3]))
{
   # do something...
}

如果你不知道有多少 $ VAR1 ... $ VARN 你有,你只能做到动态涉及要么循环或的eval ,如果你需要处理字符串或数字键依赖。这已被提问和回答:

If you don't know the how many $var1 ... $varN you have, you can only do it dynamically which involves either looping or eval and depends if you need to deal with string or numerical keys. This has been already asked and answered:

如果您担心速度,例如如果数组始终是相同的,但你需要经常查询,创建一个索引:第一,具有复合键,因此您可以更方便地查询。这可以通过存储所有键在遍历数组递归要么完成了:

If you are concerned about speed, e.g. if the array is always the same but you need to query it often, create a index first that has compound keys so you can more easily query it. That could be either done by storing all keys while traversing the array recursively:

class CompoundKeys extends RecursiveIteratorIterator
{
    private $keys;
    private $separator;
    public function __construct($separator, RecursiveIterator $iterator, $mode = RecursiveIteratorIterator::SELF_FIRST, $flags = 0)
    {
        $this->separator = $separator;
        parent::__construct($iterator, $mode, $flags);
    }
    public function current()
    {
        $current = parent::current();
        if (is_array($current))
        {
            $current = array_keys($current);
        }
        return $current;
    }
    public function key()
    {
        $depth = $this->getDepth();
        $this->keys[$depth] = parent::key();
        return implode('.', array_slice($this->keys, 0, $depth+1));
    }
}

用法:

$it = new CompoundKeys('.', new RecursiveArrayIterator($array));
$compound = iterator_to_array($it, 1);
isset($compound["$var1.$var2.$var3"]);

另外,这可以通过递归遍历和引用原阵列值来完成:

Alternatively this can be done by traversing recursively and referencing the original arrays values:

/**
 * create an array of compound array keys aliasing the non-array values
 * of the original array.
 *
 * @param string $separator
 * @param array $array
 * @return array
 */
function array_compound_key_alias(array &$array, $separator = '.')
{
    $index = array();
    foreach($array as $key => &$value)
    {
        if (is_string($key) && FALSE !== strpos($key, $separator))
        {
            throw new InvalidArgumentException(sprintf('Array contains key ("%s") with separator ("%s").', $key, $separator));
        }
        if (is_array($value))
        {
            $subindex = array_compound_key_alias($value, $separator);
            foreach($subindex as $subkey => &$subvalue)
            {
                $index[$key.$separator.$subkey] = &$subvalue;
            }
        }
        else
        {
            $index[$key] = &$value;
        }
    }
    return $index;
}

用法:

$index = array_compound_key_alias($array);
isset($index["$var1.$var2.$var3"]);

这篇关于检查是否数组多维数组中存在 - 无循环 - 深度未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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