简体   繁体   中英

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.

So I have this array in my config.php file:

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

In functions.php I have:

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.

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.

index is the key of your sub-array, not its value in_array() will look for its values in the array, not its indexes.

You can use array_key_exists() or isset() instead. When using isset() , you check if the index of the array is set.

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
  • array_key_exists() will tell you definitively if the index exists in the array or not, even if the value is null or not

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.

$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? If so, why?

You can not use in_array() function for multidimensional array. Instead, you can use array_key_exists() to check whether a key exists or not.

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. in_array checks the values. 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. Rather use array_key_exists() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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