PHP in_array() Function

What is PHP in_array() Function?

If you want to check whether a value or a subarray exist in an array, use in_array() function. You have option to find a value that is not same type as the searching value. So, the function can find the integer 10 even though you look for string “10”.

Syntax:

in_array(value_to_search, array, type_match)

Parameters:

The in_array() function has two required parameters and one optional parameter-

value_to_search (Required): The value to search with. It can be any type of data – string, integer, float, boolean, or even an array. The function will look for this parameter in the “array” parameter.

array (Required): The array to be searched inside. Check example 1.

type_match (Optional): It is a Boolean value-

  • If you set FALSE, the function finds a value in the array regardless of both value and the element type are of same type or not. So, the function can find string “10” if the array contains integer 10. It is the default value. So, the function performs a loose comparison (==) in this case. Check example 2.
  • If you set TRUE, the function finds a value in the array only if both value and the element type are of same type. So, the function can’t find string “10” if the array contains integer 10. So, the function performs a strict comparison (===) in this case. Check example 3.

Return Values:

The function returns-

  • TRUE – if it finds the value.
  • FALSE – if it doesn’t find the value.

Examples:

Example 1: Checking a value in an array with in_array() usage –

<?php
$languages = array("PHP", "Python", "JAVA", "Ruby");
echo in_array("Python", $languages) ? "Match found in the array." : "Match not found in the array.";
?>

Output:

Match found in the array.

Explanation:

The function get the matching string “Python” in the array element.

Example 2: Using “FALSE” as the 3rd parameter of in_array() function-

<?php
$languages = array("PHP", "Python", "JAVA", "Ruby", "5");
echo in_array(5, $languages, FALSE) ? "Match found in the array." : "Match not found in the array.";
?>

Output:

Match found in the array.

Explanation:

The function checks an case insensitive search when FALSE id used in the 3rd parameter. That’s why, it considers integer 5 same as string “5” and we see the match found.

Example 3: Using “TRUE” as the 3rd parameter of in_array() function –

<?php
$languages = array("PHP", "Python", "JAVA", "Ruby", 5);
echo in_array("5", $languages, TRUE) ? "Match found in the array." : "Match not found.";
echo "<br />";
echo in_array(5, $languages, TRUE) ? "Match found in the array." : "Match not found.";
?>

Output:

Match not found.
Match found in the array.

Explanation:

When TRUE is used in the 3rd parameter, the function only finds matches if both the value and the element are the same type. That’s why line 3 returns “Match not found.” and line 5 returns “Match found in the array.”.

Example 4: Checking a value which is part of an element of an array with in_array() function-

<?php
$languages = array("PHP language", "Python", "JAVA", "Ruby");
echo in_array("PHP", $languages) ? "Match found in the array." : "Match not found in the array.";
?>

Output:

Match not found in the array.

Explanation:

The function returns true if it finds an element that is “PHP” only, not “PHP” is the part of the element. That’s why it prints – “Match not found in the array.”.

Example 5: Checking a value inside an associative array with in_array() function-

<?php
$languages = array(1=>"PHP", 2=>"Python", 3=>"JAVA", 4=>"Ruby");
echo in_array("Python", $languages) ? "Match found in the array." : "Match not found in the array.";
?>

Output:

Match found in the array.

Explanation:

The function can find the value “Python” in the second item of the array.

Example 6: Checking a value inside a multi-dimensional array with in_array() function-

<?php
$arr1 = array("PHP", "Python", "JAVA", "Ruby");
$arr2 = array("MySQL", "PostgreSQL", "MariaDB");
$languages = array($arr1, $arr2, "C#");
echo in_array("PHP", $languages) ? "Match found in the array." : "Match not found in the array.";
echo "<br />";
echo in_array("C#", $languages, FALSE) ? "Match found in the array." : "Match not found in the array.";
?>

Output:

Match not found in the array.
Match found in the array.

Explanation:

As “PHP” is not in the first level of the multi-dimensional array $language, the in_array() function can’t find this.

But, as “C#” is in the first level of the multi-dimensional array $language, the in_array() function can’t find this.

Example 7: Checking an array inside a multi-dimensional array with in_array() function-

<?php
$arr1 = array("PHP", "Python", "JAVA", "Ruby");
$arr2 = array("MySQL", "PostgreSQL", "MariaDB");
$languages = array($arr1, $arr2, "C#");
echo in_array($arr1, $languages) ? "Match found in the array." : "Match not found in the array.";
echo "<br />";
$arr1 = array("PHP", array("Python"));
$arr2 = array("MySQL", "PostgreSQL", "MariaDB");
$languages = array($arr1, $arr2, "C#");
echo in_array($arr1, $languages) ? "Match found in the array." : "Match not found in the array.";
?>

Output:

Match found in the array.
Match found in the array.

Explanation:

WIth in_array() function, you can check whether an entire array exists in another array (Check line 5). WIth in_array() function, you can even check whether an entire multi-dimensional array exists in another array (Check line 10).

Example 8: Checking a value of different case inside an element of an array with in_array() function-

<?php
$languages = array("PHP", "Python", "JAVA", "Ruby");
echo in_array("php", $languages) ? "Match found in the array." : "Match not found in the array.";
?>

Output:

Match not found in the array.

Explanation:

Though the string “PHP” exists in the array, but, the function can’t find match it with “php” as bo values are not in same case.

Practical Usages of PHP in_array() Function:

You can use this function in different situations in your coding including-

  • You can use it to validate user input against a number of allowed values.
  • You can use function to allow selected users to view certain content or part of a content.

Notes on PHP in_array() Function:

  • If the searching value is a part of an array element, the function consider it not found. Check example 4.
  • This function can also search value in an associative array. Check example 5.
  • The function can’t find a value inside multi-dimensional array if it is not in level 1. Check example 6.
  • But, this function can help you to search an array (or a multi-dimensional array) as a value in a multi-dimensional array. Check example 7.
  • The function performs string search case-sensitively. So, if the string exists in the array but in mismatching case, the function can’t find the value. For example, the function doesn’t find “PHP” in the array that contains “php”. Check example 8.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP in_array() Function

in_array() is a very handy PHP built-in array function. Instead of writing loop to check whether a value exist in an array which would become inefficient, this function does it in just one line. Developers use this to check whether a value exists in an array. But, you should consider the value type when using this function.

Reference:

https://www.php.net/manual/en/function.in-array.php