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问题,您展示了错误实现可能发生的问题的解决方案。