带有关联数组的in_array()

时间:2015-07-03 17:42:46

标签: php arrays

我正在尝试从我创建的电子邮件白名单中获取值,并将其与用户在注册时使用白名单中包含的电子邮件进行比较。

阵列看起来像这样:

Array ( [0] => Array ( [email] => @hca.com ) [1] => Array ( [email] => @hospitaldatasolutions.com ) )

我的代码:

$test = '@hca.com';
$get_whitelist = $mysqli->query('SELECT email FROM email_whitelist');
$white_list_arr = mysqli_fetch_all($get_whitelist, MYSQLI_ASSOC);
if(in_array($test, $white_list_arr, true) == true){
print_r($white_list_arr);}

当该字符串确实存在于数组中时,if语句当前返回false。上面的数组似乎是多维的,我有一种感觉,这就是为什么in_array不起作用,但我想有第二个意见。我在另一篇文章中读到了“进一步嵌套”的内容,但我并不理解这意味着什么。

:: EDIT ::

这是正确答案:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

0 个答案:

没有答案