The php in_array() function allows us to check if a value is in a php array. The php in_array() function returns true if the value is in the array, and false if the value is not in the array.

From the php documentation, the php in_array() function takes 3 parameters:

in_array(mixed $needle, array $haystack, bool $strict = false): bool

In the syntax above, the needle is the value we want to check and the haystack is the array.

The third parameter strict is used if we want to use loose comparison (==) or strict comparison (===). Depending on how complex the structure of your array is, you will need to use the strict parameter to force strict comparison.

Using php in_array() To Check if String is in an Array of Strings

Let’s say we have the following array in php.

$sample_array = array("The", "polar", "bear", "is", "very", "big");

We can use the php in_array() function to check if the values “polar” and “express” are in the array. To do this, we call the php in_array() function in the following way, and echo the result.

$sample_array = array("The", "polar", "bear", "is", "very", "big");

// using json_encode because the php echo function prints "1" for true and "" for false

echo json_encode(in_array("polar", $sample_array)); // true
echo json_encode(in_array("express", $sample_array)); // false

As expected, “polar” was found in the array, and “express” was not found in the array. When comparing strings, the php in_array() function is case sensitive. So, if we wanted to check if the value “Polar” was in the array, the function would return false. This is shown in the following example.

$sample_array = array("The", "polar", "bear", "is", "very", "big");

echo json_encode(in_array("Polar", $sample_array)); // false

php in_array() Loose Comparison Example

In the example above, we used the php in_array() function to check if a string was in a list of strings. We didn’t need to use the strict parameter because both the needle and haystack values were the same type.

Let’s say we have the following array. This array has different types.

$sample_array = array("The", 2, "3", "three", "six", "Yankees", "sun");

Let’s see if a few different values are in this array with in_array() and see the effects of using loose comparison with in_array().

$sample_array = array("The", 2, "3", "three", "six", "Yankees", "sun");

echo json_encode(in_array("The", $sample_array));  // true  
echo json_encode(in_array("two", $sample_array));  // false 
echo json_encode(in_array(2, $sample_array));  // true 
echo json_encode(in_array(3, $sample_array));  // true  
echo json_encode(in_array("3", $sample_array));  // true 

What is interesting here is that when we check to see if the number 3 is in the array, the in_array() function returns true. This is because when we compare 3 and “3” using loose comparison, these are equal.

However, this is not the case with strict comparison.

php in_array() Strict Comparison Example

In the example above, we saw that when we use loose comparison in the php in_array() function, we can receive interesting results.

To use strict comparison, we pass true to the strict parameter in the in_array() function.

Let’s take the same array above and check the same values to see if they are in the array with strict comparison.

$sample_array = array("The", 2, "3", "three", "six", "Yankees", "sun");

echo json_encode(in_array("The", $sample_array, true));  // true    
echo json_encode(in_array("two", $sample_array, true));  // false   
echo json_encode(in_array(2, $sample_array, true));  // true        
echo json_encode(in_array(3, $sample_array, true));  // false        
echo json_encode(in_array("3", $sample_array, true));  // true      

In this example, we see that 3 is not equal to “3” with strict comparison, because 3 is an integer and “3” is a string.

Using in_array() to find an array in an array

We can also use the in_array() function to see if an array is inside another array.

Let’s say I have the following array of arrays in my php code:

$sample_array_of_arrays = [
    [0,1,2,3],
    [4,5,6,7],
    [8,9,10],
    ["0","1","2"]
];

We can use the in_array() function to check if the array [8,9,10] is in our array of arrays using the following code:

$sample_array_of_arrays = [
    [0,1,2,3],
    [4,5,6,7],
    [8,9,10],
    ["0","1","2"]
];

echo json_encode(in_array([8,9,10], $sample_array_of_arrays)) // true   

As expected, the array [8,9,10] is in our array of arrays and the in_array() function returned true.

Can I use in_array() to find an object in an array?

The in_array() function is very useful in many cases. However, when looking to find if an object is in an array of objects, we need to be careful.

We can use loose comparison to find if an object has the same properties as another object in an array, but if we want to find out if an instance is the same, we need to use strict comparison.

Let’s say I have the following simple class:

class Person
{
	private $id;
	private $name;
	private $age;


	public function __construct($id, $name, $age)
	{
		$this->id = $id;
		$this->name = $name;
		$this->age = $age;
	}
}

I can instantiate a few instances of this object, create an array, and check to see what’s in the array using our in_array() function below:

class Person
{
	private $id;
	private $name;
	private $age;


	public function __construct($id, $name, $age)
	{
		$this->id = $id;
		$this->name = $name;
		$this->age = $age;
	}
}

$bob = new Person(1, 'Bob', 20);
$sally = new Person(2, 'Sally', 35);
$mike = new Person(3, 'Mike', 40);
$patricia = new Person(4, 'Patricia', 15);

$people = [
	$bob,
	$sally,
	$mike,
	$patricia,
];

echo json_encode(in_array(new Person(4, 'Patricia', 15),$people)); // true
echo json_encode(in_array(new Person(4, 'Patricia', 15), $people, true)); // false
echo json_encode(in_array($patricia, $people, true)); // true

As you can see, there is a difference here when using loose comparison and strict comparison – when using loose comparison, the in_array() function is checking only the values of each of the objects, where with strict comparison, the in_array() function is checking the if the instance is in the array.

Hopefully you now know how to use the php in_array() function to check if a value is in an array.

Categorized in:

PHP,

Last Update: March 21, 2024