Q:

Why won't this script recognize an array as an array?

I wrote this script in PHP:
$menuen = array(
    "didattica" => "program",
    "procedure" => "policies",
    "news" => "news",
);
$menuit = array(
    ...
);
function getName ($link) {
    if ($lang === "en") {
        if (in_array($link, array_keys($menuen))) {
            return $menuen[$link];
        }
        else {
            return ucfirst(str_replace("_", " ", $link));
        }
    }
    else {
        if (in_array($link, array_keys($menuit))) {
            return $menuit[$link];
        }
        else {
            return ucfirst(str_replace("_", " ", $link));
        }
    }
}

$lang is en.
But when I call the function like as getName("didattica");, the script throws following errors:
Warning:  array_keys() expects parameter 1 to be array, null given in [script path] on line [if (in_array($link, array_keys($menuen))) {]

Warning:  in_array() expects parameter 2 to be array, null given in [script path] on line [if (in_array($link, array_keys($menuen))) {]

So, the script doesn't recognize $menuen as an array.
Why?
Thank you in advance

A:

The problem is related to the scope of the variables, you need to identify its as global first.
Change the function for this one:
function getName ($link) {
    global $menuen,$menuit;

    if ($lang === "en") {
        if (in_array($link, array_keys($menuen))) {
            return $menuen[$link];
        }
        else {
            return ucfirst(str_replace("_", " ", $link));
        }
    }
    else {
        if (in_array($link, array_keys($menuit))) {
            return $menuit[$link];
        }
        else {
            return ucfirst(str_replace("_", " ", $link));
        }
    }
}

A:

$menuen/$menuit is outside of the scope of the function. You can do 1 of 2 things.
Use global:
function getName ($link) {
    global $menuen,$menuit;
    ....
}

Pass the variable as a function parameter:
function getName ($link,$menuen,$menuit) {
    ....
}

getName($link,$menuen,$menuit);

The second is the preferred method.

