PHP in_array() Function (original) (raw)
Last Updated : 02 Jun, 2025
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,arrayneedle, array needle,arrayhaystack, 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 needleisfoundinneedle is found in needleisfoundinhaystack.
- 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 `
`
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 `
`
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 `
`
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 `
`
Output
bool(false) bool(true)
4. Searching in Multidimensional Arrays
in_array() does not search inside nested arrays automatically:
PHP `
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 `
names=arraycolumn(names = array_column(names=arraycolumn(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.