The in_array() function in PHP is a built-in function that is used to check if a specific value exists within an array and returns a boolean result. Returns true if the value is found and false if the value is not found.
Syntax:
bool in_array(mixed $needle, array $haystack, bool $strict = false)
In this syntax:
- $needle (mixed): The value to search for in the array.
- $haystack (array): The array in which to search.
- $strict (bool, optional): If set to true, the function will also check the types of the $needle and the array elements.
- Defaults to false (non-strict comparison).
Return Value
- Returns true if $needle is found in $haystack.
- Returns false if $needle is not found.
How in_array() Works?
The in_array() function in the PHP works in the following ways:
- By default, in_array() performs a loose comparison (==) between $needle and the array elements.
- If $strict is true, it performs a strict comparison (===) which checks both the value and the type.
- This behavior is important when searching in arrays with mixed types (e.g., strings, integers, floats).
Now, let us understand with the help of the example:
PHP
$fruits = ["apple", "banana", "orange"];
if (in_array("banana", $fruits)) {
echo "Banana is in the list!";
} else {
echo "Banana is not found.";
}
?>
OutputBanana is in the list!
1. Using in_array() with Numbers
This example demonstrates how in_array() checks for both numeric and string values in an array using loose comparison.
PHP
$numbers = [1, 2, 3, "4"];
var_dump(in_array(3, $numbers));
var_dump(in_array("4", $numbers));
var_dump(in_array(5, $numbers));
?>
Outputbool(true)
bool(true)
bool(false)
2. Strict Type Checking
This example illustrates how in_array() uses strict mode to compare both value and type when searching in an array.
PHP
$numbers = [1, 2, 3, "4"];
var_dump(in_array(4, $numbers, true));
var_dump(in_array("4", $numbers, true));
?>
Outputbool(false)
bool(true)
3. Case Sensitivity
This example highlights in_array()’s case-sensitive behavior when searching for string values inside an array.
PHP
$colors = ["Red", "Green", "Blue"];
var_dump(in_array("red", $colors)); // false, "red" != "Red"
var_dump(in_array("Red", $colors)); // true
?>
Outputbool(false)
bool(true)
4. Searching in Multidimensional Arrays
in_array() does not search inside nested arrays automatically:
PHP
$array = [
["id" => 1, "name" => "anjali"],
["id" => 2, "name" => "arti"]
];
var_dump(in_array("anjali", $array));
?>
To search deeply in multidimensional arrays, you need to use custom functions or array functions like array_column():
PHP
if (is_array($array)) {
$names = array_column($array, 'name');
if (in_array("anjali", $names)) {
echo "anjali is found!";
}
} else {
echo "Input data is not valid.";
}
?>
OutputInput data is not valid.
Common Use Cases for in_array()
- Form validation: Check if a submitted value is among the allowed options.
- User permissions: Verify if a user role exists in the allowed roles array.
- Filtering: Decide whether to include/exclude elements based on a list.
- Control flow: Execute code conditionally based on presence of values.
Best Practices
- Use the $strict parameter to avoid unexpected matches, especially when working with numeric strings and integers.
- For multidimensional arrays, consider flattening or using array functions like array_column().
- For case-insensitive checks, manually normalize the case before searching.
- Remember that in_array() only searches the first-level array elements.
Conclusion
The PHP in_array() function is a straightforward and handy tool for checking if a value exists within an array. Its flexibility with the optional strict type checking and compatibility with various data types make it a staple in PHP programming. Remember its limitations in multidimensional arrays and case sensitivity to use it effectively.
Similar Reads
PHP array_chunk() Function The array_chunk() function is an inbuilt function in PHP which is used to split an array into parts or chunks of given size depending upon the parameters passed to the function. The last chunk may contain fewer elements than the desired size of the chunk. Syntaxarray array_chunk( $array, $size, $pre
2 min read
PHP array_combine() Function The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of t
2 min read
PHP array_count_values() Function The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) Parameters: Thi
2 min read
PHP array_diff_assoc() Function This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares both the keys and values between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to there keys and values and returns the
2 min read
PHP array_diff_key() Function This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares the keys between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to their keys and returns the elements that are present
2 min read
PHP array_diff_uassoc() Function The array_diff_uassoc() function is a built-in function in PHP and is used to get the difference between one or more arrays using an user-defined function to compare the keys. This function compares both the keys and values between one or more arrays to and returns the elements from the first array
4 min read
PHP array_diff_ukey() Function The array_diff_ukey() function is an inbuilt function in PHP. It is used to compares the key two or more arrays using a user-defined function, and return an array, which is array1 and its not present any others array2, array3 or more... Syntax: array_diff_ukey($array1, $array2, $array3..., arr_diffu
4 min read
PHP array_diff() function The array_diff() is an inbuilt function in PHP ans is used to calculate the difference between two or more arrays. This function computes difference according to the values of the elements, between one or more array and return differences in the form of a new array. This function basically returns a
2 min read
PHP array_fill_keys() Function The array_fill_keys() function is a builtin function in PHP and is used to create a new array filled with the given keys and value provided as an array to the function. Syntax: array array_fill_keys ( $keys, $value ) Parameters: This function accepts two parameters, keys and their values to be prese
3 min read
PHP array_fill() function The array_fill() is an inbuilt-function in PHP and is used to fill an array with values. This function basically creates an user-defined array with a given pre-filled value. Syntax: array_fill($start_index, $number_elements, $values) Parameter: The array_fill() function takes three parameters and ar
2 min read