PHP in_array Function | Check If Element Exists in Array

The in_array Function is an inbuilt Function in PHP. It is used to check whether an element exists in an array or not. It is one of the most used functions in PHP. In this article, we will discuss the PHP in_array Function. Also, we will discuss a few examples to demonstrate it’s usage.

Syntax

bool in_array ($needle, $haystack, $strictMode = false)

Parameters

The PHP in_array Function takes in three parameters. Two parameters are mandatory and one is optional. Let’s discuss about the parameters below:

  • $needle: The needle is the first parameter function to the function. It is a mandatory parameter and specifies the element to be searched in the given array. Also, the needle can be an integer, string and even array.
  • $haystack: The haystack is also a mandatory parameter. It is the array in which to search.
  • $strictMode: The third parameter to the function is an optional parameter. It specifies whether the search will be strict or not. If the parameter is true then the function matches the value and type of the elements. However, the default value of the parameter is false.

Return Value

The PHP in_array Function returns a boolean value. If the needle is present in the haystack, then it returns true. Otherwise, it returns false.

PHP in_array Function Diagram Concatly

Examples

Let’s discuss a few examples to use the in_array Function in PHP.

Example 1: Integer Array

For instance, consider an integer array.

<?php
	$haystack = array(5, 0, 6, 21, 63, 24);
	var_dump(in_array(6, $haystack));
	/* bool(true) */

	var_dump(in_array(1, $haystack));
	/* bool(false) */
?>

In the above example, since 6 is present in the given array, the function returns true. Also, 1 is not present and it returns false.

Example 2: String Array

Consider the given string array.

<?php
	$haystack = array('This', 'is', 'a', '100');
	var_dump(in_array('This', $haystack));
	/* bool(true) */

	var_dump(in_array(100, $haystack));
	/* bool(true) */

	var_dump(in_array(100, $haystack, true));
	/* bool(false) */
?>

Similar to integers, we can perform a search using strings. Notice

Note: The array contains a string 100. The function returns true for an integer 100 as the strict mode is false. But with the strict mode set to true, the function returns false as it matches the value and data type of the elements.

Example 3: Nested Arrays

Similarly, we can check for array elements also as needle.

<?php
	$haystack = array(array('Chandler', 'Monica'), 'Ross');
	$needle = array('Monica', 'Chandler');
	var_dump(in_array($needle, $haystack));
	/* boolean(false) */

	$needle = array('Chandler', 'Monica');
	var_dump(in_array($needle, $haystack));
	/* boolean(true) */
?>

Do notice, that the order of elements in the needle array is also important. The in_array function in PHP returns true only when the inner array elements are in the correct order.

Conclusion

We discussed the in_array Function in PHP. It is very useful in checking whether an element exists in an array or not. You can learn more about it on the Official Documentation of PHP. Also, you can read about other Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *