[thelist] PHP & arrays

Steve Lewis slewis at macrovista.net
Tue May 7 13:41:00 CDT 2002


Mark Joslyn wrote:

>Hello List!
>
>I am trying to search through an array to determine if 4 distinct names
>appear in that array. If at least one of the names does not appear in the
>array, the error message gets called. But, if one of the names does appear,
>the error message is ignored. The in_array function works when there is one
>variable, but not multiple ones.
>
>Am I using the wrong function, or is my syntax messed up:
>
>
>if (!(in_array("Jim" or "John" or "Frank" or "Bob", $myEmailP)))
>		error("Please enter a valid employee name.");
>
>
>
>Any help would be appreciated.
>
>Thanks,
>
>markJ
>
some suggested solutions include...

PHP3

    // $myEmailP is an array and has already been defined
    $reqNames = array("Jim", "John", "Frank", "Bob");

    if (count(array_intersect($reqNames, $myEmailP)) == 0)
    {
        error("Please enter a valid employee name.");
    }

PHP4

    // $myEmailP is an array and has already been defined
    $reqNames = array("Jim", "John", "Frank", "Bob");
    $name_is_found = FALSE;

    // foreach opperates on a copy of $reqNames so we don't have to
worry about reseting it
    foreach($reqNames as $name)
    {
        if (in_array($name, $myEmailP))
        {
            $name_is_found = TRUE;
            break;
        }
    }
    if (!($name_is_found))
    {
        error("Please enter a valid employee name.");
    }

--Steve




More information about the thelist mailing list