How to Check if an Array Contains a Value in PHP with in_array
in_array($needle, $haystack, true) in PHP returns true when the array contains the value and false when it doesn't. The third argument, true, makes it compare types as well as values, which you nearly always want.
Syntax
$<found> = in_array(<needle>, <haystack>, true);
| Part | What it does |
|---|---|
<needle> | The value to look for. |
<haystack> | The array to look in. |
true | Strict mode: the value must also have the same type, as with ===. |
$<found> | true or false. |
The PHP manual: in_array "Searches for needle in haystack using loose comparison unless strict is set", and "If the third parameter strict is set to true then the in_array() function will also check the types of the needle in the haystack."
The Example
The example looks for two URLs in a list of allowed audiences, strictly, one that's in the list and one that isn't. Then it looks for the string '10' in an array of numbers, first without and then with strict mode. var_dump prints each result with its type.
Prerequisites
- PHP. This guide uses PHP 8.6.0.
Step 1: Write the Code
Create a file named main.php:

<?php
$audiences = ['https://photos.example.org', 'https://videos.example.org'];
var_dump(in_array('https://photos.example.org', $audiences, true));
var_dump(in_array('https://music.example.org', $audiences, true));
$ids = [10, 20, 30];
var_dump(in_array('10', $ids));
var_dump(in_array('10', $ids, true));
Step 2: Run It
php main.php
bool(true)
bool(false)
bool(true)
bool(false)

The command is the same in zsh, bash and PowerShell.
What Happened
- The photos URL was found and the music URL wasn't, as you'd expect.
- The string
'10'was found in[10, 20, 30]without strict mode. A loose comparison treats the string'10'and the number10as equal. - With strict mode, it wasn't found, because a string is never identical to a number.
For an allow-list check, such as whether a token's audience is one your API accepts, use strict mode so values of a different type can't slip through.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving out the third argument | Values of another type can match, such as '10' and 10 | Pass true |
| Swapping the first two arguments | PHP looks for the array inside the value and fails with a TypeError | The value comes first, the array second |
Using in_array on a very large array in a loop | Each call looks through the whole array | Flip the array once with array_flip and use isset on its keys |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
in_array(): Argument #2 ($haystack) must be of type array, string given | The second argument isn't an array | Pass the array second, or convert the value with (array) |
| A value matches that shouldn't | The check isn't strict | Add true as the third argument |
FAQ
How do I get the position of the value instead?
Use array_search, which returns the key of the value, or false when it isn't there. It takes the same strict argument.
Is in_array case-sensitive for strings?
Yes. 'Photos' and 'photos' are different strings in both modes.
Conclusion
in_array($value, $array, true) tells you whether an array contains a value, comparing types as well as values. Pass true unless you have a reason not to.
Additional Resources
- in_array and comparison operators, in the PHP manual.
