Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查多维数组中是否存在数组-无循环-未知深度_Php_Arrays_Search_Multidimensional Array - Fatal编程技术网

Php 检查多维数组中是否存在数组-无循环-未知深度

Php 检查多维数组中是否存在数组-无循环-未知深度,php,arrays,search,multidimensional-array,Php,Arrays,Search,Multidimensional Array,我需要快速查找,以确定数组中是否存在数组。如果我知道阵列的深度,这将是简单而快速的 $heystack['lev1']['lev2']['lev3'] = 10; // $heystack stores 10,000s of arrays like this if(isset($heystack[$var1][$var2][$var3])) do something... 如果您不知道深度,您将如何动态执行此操作?对于我的应用程序来说,每一级的循环和搜索速度都太慢。您需要某种循环,但不需要遍

我需要快速查找,以确定数组中是否存在数组。如果我知道阵列的深度,这将是简单而快速的

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

if(isset($heystack[$var1][$var2][$var3])) do something...
如果您不知道深度,您将如何动态执行此操作?对于我的应用程序来说,每一级的循环和搜索速度都太慢。

您需要某种循环,但不需要遍历整个深度。您只需使用一个函数,该函数相当于
$heystack[$var1][$var2][$var3]
,但动态:

$heystack['lev1']['lev2']['lev3'] = 10;

echo getElement($heystack, array('lev1', 'lev2', 'lev3')); // you could build second parameter dynamically

function getElement($array, $indexes = array())
{
    foreach ($indexes as $index) {
        $array = $array[$index];
    }

    return $array;
}

// output: 10

您需要加入一些防御机制,使函数更加健壮(对于不存在的元素/索引),但这是基本方法。

您的问题已经有了答案:

if (isset($heystack[$var1][$var2][$var3]))
{
   # do something...
}
如果您不知道有多少
$var1$varN
您有,您只能动态执行,这涉及循环或
eval
并取决于您是否需要处理字符串键或数字键。这一点已经得到了询问和回答:

  • 循环和求值:(这只是其中的一个)
如果您担心速度,例如,如果数组总是相同的,但需要经常查询,请先创建一个具有复合键的索引,以便更轻松地查询它。这可以通过在递归遍历数组时存储所有键来实现:

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"]);
$index = array_compound_key_alias($array);
isset($index["$var1.$var2.$var3"]);
或者,也可以通过递归遍历和引用原始数组值来实现:

/**
 * 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;
}
用法:

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

是否要检查
$heystack[$var1][$var2][$var3]
是否为数组?像使用
is_数组($heystack[$var1][$var2][$var3])
?如果你不知道元素的深度/位置,你可能会尝试“不知道深度”,你的意思是你不知道$var1 2和3的位置吗?也许对你来说
在_数组中()
(允许在数组中搜索数组)会有所帮助。。。关于深度。。。数组中的所有数组深度都相同吗?我只想提到isset($heystack[$var1][$var2][$var3]对于键为$var1和$var2但不是$var3的给定数组,将失败。相反,$var3将转换为字符串,这将返回$var2中值的第一个字符。因此isset将返回true,但$key3不存在!//用于了解深度和键if(is_数组($heystack[$var1][$var2])&&isset($heystack[$var1][$var2][$var3]))…当元素存在但设置为
null
时,实际上
isset
也将返回
false
。这是常见的警告。对于isset问题,您展示了错误实现可能发生的问题的解决方案。