PHP in_array() Function
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.";
}
?>
Output
Banana 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));
?>
Output
bool(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));
?>
Output
bool(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
?>
Output
bool(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));
?>
Output
bool(false)
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.";
}
?>
Output
Input 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.