Using PHP in_array() Function: Syntax, Parameters, Return Values, and Examples

This article explains the PHP in_array() function, covering its syntax, parameter details, return values, and provides three practical code examples—including basic usage, strict mode comparison, and searching within multidimensional arrays—to help developers efficiently check element existence in arrays.

php Courses
php Courses
php Courses
Using PHP in_array() Function: Syntax, Parameters, Return Values, and Examples

In PHP, the built-in in_array() function checks whether a specific value exists in a given array, returning true if found and false otherwise.

Syntax:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Parameter Description:

$needle

: the value to search for, can be of any type. $haystack: the array to search in. $strict (optional): if set to true, in_array() also checks the type of the needle.

Return Value:

Returns TRUE if the needle is found; otherwise returns FALSE.

Below are several examples demonstrating how to use in_array().

Example 1: Basic Usage

$fruits = array("apple", "banana", "orange", "grape");
if (in_array("banana", $fruits)) {
    echo "Found banana!";
} else {
    echo "Did not find banana!";
}

Output: Found banana! Explanation: The array $fruits contains "banana", so the function returns true and the message is printed.

Example 2: Using Strict Mode

$numbers = array(1, 2, "3", 4, 5);
if (in_array("3", $numbers, true)) {
    echo "Found 3!";
} else {
    echo "Did not find 3!";
}

Output: Did not find 3! Explanation: Although the value "3" exists, it is a string while the array also contains an integer 3; strict mode requires both value and type to match, so the function returns false.

Example 3: Searching in Multidimensional Arrays

$people = array(
    array("name" => "John", "age" => 20),
    array("name" => "Mary", "age" => 30),
    array("name" => "David", "age" => 25)
);
if (in_array(array("name" => "Mary", "age" => 30), $people)) {
    echo "Found Mary!";
} else {
    echo "Did not find Mary!";
}

Output: Found Mary! Explanation: The multidimensional array $people contains an element that exactly matches the associative array passed as the needle, so the function returns true.

Summary

The in_array() function is a practical PHP utility for quickly checking the presence of a specific element in an array, with optional strict type comparison and support for multidimensional arrays.

In everyday PHP development, this function helps resolve element‑existence checks efficiently.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendphp-functionsarray_searchin_array
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.