【发布时间】:2014-01-02 17:30:42
【问题描述】:
前几天我问了一个与此相关的问题,我得到了答案,但它没有达到我想要的效果。这是我遍历多维关联数组,检查数组中是否存在键的方法(来自我上一个问题的答案):
private function checkKeyIsInArray($dataItemName, $array)
{
foreach ($array as $key => $value)
{
// convert $key to string to prevent key type convertion
echo '<pre>The key: '.(string) $key.'</pre>';
if ((string)$key == $dataItemName)
return true;
if (is_array($value))
return $this->checkKeyIsInArray($dataItemName, $value);
}
return false;
}
这是我的数组结构:
Array (
[0] => Array ( [reset_time] => 2013-12-11 22:24:25 )
[1] => Array ( [email] => someone@example.com )
)
该方法遍历第一个数组分支,但不遍历第二个。有人可以解释为什么会这样吗?看来我错过了什么。
【问题讨论】:
-
你能把输出放在你的问题中吗?
-
一个多维数组的例子...
-
为什么不使用 PHP 内置的 array_key_exists 函数?
-
我认为这个链接有更聪明的解决方案来检查数组键的存在。 stackoverflow.com/a/2948985/1202487
标签: php multidimensional-array nested associative