0

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.

Qirel
  • 21,424
  • 7
  • 36
  • 54

4 Answers4

4

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?

Qirel
  • 21,424
  • 7
  • 36
  • 54
  • array_key_exists() solved for me, thank you! Is there a way to remove global? The only other way of replacing it would be passing the array along when calling the function, correct? – Guilherme Corrêa Peralta Oct 31 '18 at 14:44
1

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";
    }
}
akshaypjoshi
  • 1,180
  • 1
  • 11
  • 22
0

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";
    }
}
ccKep
  • 5,500
  • 15
  • 28
0

You are searching for values by using in_array() you can't use that. Rather use array_key_exists().