PHP in_array

Summary: in this tutorial, you will learn how to use the PHP in_array() function to check if a value exists in an array.

Introduction to the PHP in_array() function

The in_array() function returns true if a value exists in an array. Here’s the syntax of the in_array() function:

in_array ( mixed $needle , array $haystack , bool $strict = false ) : boolCode language: PHP (php)

In this syntax:

  • $needle is the searched value.
  • $haystack is the array to search.
  • $strict if the $strict sets to true, the in_array() function will use the strict comparison.

The in_array() function searches for the $needle in the $haystack using the loose comparison (==). To use the strict comparison (===), you need to set the $strict argument to true.

If the value to check is a string, the in_array() function will search for it case-sensitively.

The in_array() function returns true if the $needle exists in the $array; otherwise, it returns false.

PHP in_array() function examples

Let’s take some examples of using the in_array() function.

1) Simple PHP in_array() function examples

The following example uses the in_array() function to check if the value 'update' is in the $actions array:

<?php

$actions = [
	'new',
	'edit',
	'update',
	'view',
	'delete',
];

$result = in_array('update', $actions);

var_dump($result); // bool(true)Code language: HTML, XML (xml)

It returns true.

The following example returns false because the publish value doesn’t exist in the $actions array:

<?php

$actions = [
	'new',
	'edit',
	'update',
	'view',
	'delete',
];

$result = in_array('publish', $actions);

var_dump($result); // bool(false)
Code language: HTML, XML (xml)

The following example returns false because the value 'New' doesn’t exist in the $actions array. Note that the in_array() compares the strings case-sensitively:

<?php

$actions = [
	'new',
	'edit',
	'update',
	'view',
	'delete',
];

$result = in_array('New', $actions);

var_dump($result); // bool(false)
Code language: HTML, XML (xml)

2) Using PHP in_array() function with the strict comparison example

The following example uses the in_array() function to find the number 15 in the $user_ids array. It returns true because the in_array() function compares the values using the loose comparison (==):

<?php

$user_ids = [10, '15', '20', 30];

$result = in_array(15, $user_ids);

var_dump($result); //  bool(true)Code language: HTML, XML (xml)

To use the strict comparison, you pass false to the third argument ($strict) of the in_array() function as follows:

<?php

$user_ids = [10, '15', '20', 30];

$result = in_array(15, $user_ids, true);

var_dump($result); //  bool(false)Code language: HTML, XML (xml)

This time the in_array() function returns false instead.

3) Using PHP in_array() function with the searched value is an array example

The following example uses the in_array() function with the searched value is an array:

<?php

$colors = [
	['red', 'green', 'blue'],
	['cyan', 'magenta', 'yellow', 'black'],
	['hue', 'saturation', 'lightness']
];

if (in_array(['red', 'green', 'blue'], $colors)) {
	echo 'RGB colors found';
} else {
	echo 'RGB colors are not found';
}Code language: HTML, XML (xml)

Output:

RGB colors found

4) Using PHP in_array() function with an array of objects example

The following defines the Role class that has two properties $id and $name:

<?php

class Role
{
	private $id;

	private $name;

	public function __construct($id, $name)
	{
		$this->id = $id;
		$this->name = $name;
	}
}Code language: HTML, XML (xml)

This example illustrates how to use the in_array() function to check if a Role object exists in an array of Role objects:

<?php
// Role class

$roles = [
	new Role(1, 'admin'),
	new Role(2, 'editor'),
	new Role(3, 'subscribe'),
];

if (in_array(new Role(1, 'admin'), $roles)) {
	echo 'found it';
}Code language: HTML, XML (xml)

Output:

found it!

If you set the $strict to true, the in_array() function will compare objects using their identities instead of values. For example:

// Role class

$roles = [
	new Role(1, 'admin'),
	new Role(2, 'editor'),
	new Role(3, 'subscribe'),
];

if (in_array(new Role(1, 'admin'), $roles, true)) {
	echo 'found it!';
} else {
	echo 'not found!';
}Code language: PHP (php)

Output:

not found!

Summary

  • Use PHP in_array() function to check if a value exists in an array.
Did you find this tutorial useful?