简体   繁体   English

为什么PHP函数找不到in_array项目?

[英]Why PHP function can't find item in_array?

I googled and search StackOverflow many times an read the whole PHP manual for in_array() but still stuck with what I thought it would be a very simple task. 我用Google搜索和搜索StackOverflow多次,以阅读整个PHP手册中的in_array()但仍然坚持我认为这将是一个非常简单的任务。

So I have this array in my config.php file: 所以我的config.php文件中有这个数组:

$page_access = array(
    'index' => array('1', '2', '3'),
    'users' => array('4', '5', '6')
);

In functions.php I have: 在functions.php中,我有:

include 'config.php';

function level_access($page){
    global $page_access;
    if(in_array($page, $page_access)){
        echo "yes";
    } else {
        echo "no";
    }
}

level_access('index');

I expected to get "yes" as an output because then I would do something else in the function, but I'm stuck with a "no" output, no matter what I do. 我希望得到“ yes”作为输出,因为那样的话我会在函数中做其他事情,但是不管我做什么,我都会坚持使用“ no”输出。

I already tried a print_r($page_access) INSIDE the function just to check if it can read the array, and IT DOES return me the whole array (which means the function is reaching the outside array), but every time the answer to in_array() is NO. 我已经尝试过在函数中使用print_r($page_access)只是为了检查它是否可以读取数组,并且它确实将整个数组返回给我(这意味着函数到达了外部数组),但是每次都对in_array()否。

index is the key of your sub-array, not its value in_array() will look for its values in the array, not its indexes. index是子数组的键,而不是其值in_array()会在数组中查找其值,而不是其索引。

You can use array_key_exists() or isset() instead. 您可以改用array_key_exists()isset() When using isset() , you check if the index of the array is set. 使用isset() ,请检查是否设置了数组的索引。

if (array_key_exists($page, $page_access)) {
    echo "yes";
}

// Or..

if (isset($page_access[$page])) {
    echo "yes";
}
  • isset() will tell you if the index of the array is set, and its value is not null isset()会告诉您是否设置了数组的索引,并且其值不为null
  • array_key_exists() will tell you definitively if the index exists in the array or not, even if the value is null or not array_key_exists()将明确告诉您索引是否存在于数组中,即使该值是否为null

See this live demo . 观看此现场演示

That being said, usage of the global keyword is discouraged , and you should instead pass the variable as an argument to the function. 话虽如此, 不鼓励使用global关键字,而应该将变量作为参数传递给函数。

$page_access = array(
    'index' => array('1', '2', '3'),
    'users' => array('4', '5', '6')
);

function level_access($page, $page_access) {
    // Either isset() or array_key_exists() will do - read their docs for more info
    // if (array_key_exists($page, $page_access)) {
    if (isset($page_access[$page])) {
        echo "yes";
    } else {
        echo "no";
    }
}
level_access($page, $page_access);

See Are global variables in PHP considered bad practice? 请参阅是否将PHP中的全局变量视为不良做法? If so, why? 如果是这样,为什么?

You can not use in_array() function for multidimensional array. 您不能将in_array()函数用于多维数组。 Instead, you can use array_key_exists() to check whether a key exists or not. 相反,您可以使用array_key_exists()来检查密钥是否存在。

function level_access($page)
{
    global $page_access;
    if (array_key_exists($page, $page_access)) {
        echo "yes";
    } else {
        echo "no";
    }
}

index is just a key in your $pages_access array. index只是$pages_access数组中的 in_array checks the values. in_array检查值。 To fix your code: 要修复您的代码:

function level_access($page){
    global $page_access;
    if(in_array($page, array_keys($page_access))){
        echo "yes";
    } else {
        echo "no";
    }
}

You are searching for values by using in_array() you can't use that. 您正在使用in_array()搜索值,但不能使用它。 Rather use array_key_exists() . 而是使用array_key_exists()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM