faqts : Computers : Programming : Languages : PHP : kms : General

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

55 of 64 people (86%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I determine if an array contains a value?
What is the in_array() function for?

Jan 28th, 2000 00:28
Nathan Wallace, Radek Vybiral, Mark Roedel


In many cases I need in_array() function, which returns true or false if
an element is in the array.

Here is the in_array function from PHP4:

    http://www.php.net/manual/html/function.in-array.html

On PHP3, try using this code:

    function in_array($item, $my_array) {
        $found = 0;  // Not found
    
        while (list($val) = each($my_array)) {
            if ($item == $my_array[$val]) { $found = 1; }
        }
        return $found;
    }