Skip to main content

How to Check if an Array Contains a Value in PHP with in_array

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
$<found> = in_array(<needle>, <haystack>, true);
PartWhat it does
<needle>The value to look for.
<haystack>The array to look in.
trueStrict 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:

main.php, which checks with in_array and true whether the photos URL and the music URL are in an audiences array, then checks whether the string 10 is in an array of integers, without and with strict mode, printing each result with var_dump.

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​

Terminal
php main.php
Output
bool(true)
bool(false)
bool(true)
bool(false)

A zsh terminal where php main.php prints bool(true), bool(false), bool(true) and 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 number 10 as 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​

MistakeWhat happensDo this instead
Leaving out the third argumentValues of another type can match, such as '10' and 10Pass true
Swapping the first two argumentsPHP looks for the array inside the value and fails with a TypeErrorThe value comes first, the array second
Using in_array on a very large array in a loopEach call looks through the whole arrayFlip the array once with array_flip and use isset on its keys

Troubleshooting​

SymptomLikely causeFix
in_array(): Argument #2 ($haystack) must be of type array, string givenThe second argument isn't an arrayPass the array second, or convert the value with (array)
A value matches that shouldn'tThe check isn't strictAdd 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​