代码版本:PHP 7.0+
/**
* 数组检测:以'键名.键名'的方式检测数组键名是否存在
* @param array $array 数组
* @param string $name 检测键名
* @return bool 是否存在
*/
function arr_dot_has(array $array, string $name): bool
{
$keys = explode('.', $name);
$temp = $array;
foreach ($keys as $key) {
if (!isset($temp[$key])) {
return false;
}
$temp = $temp[$key];
}
return true;
}
调用示例:
$array = ['test' => [2 => [3, 4, 5]]]; echo arr_dot_has($array, 'test.2.3'); //false