PHP in_array() Function
The in_array() function in PHP is a built-in function that checks if a specific value exists in an array or not. You can use this function when you want to verify the existence of a value in an array.
In simple words, the in_array() function search an array for a specific value. The general syntax of the in_array() function in PHP is as follows:
in_array(mixed $needle, array $haystack, bool $strict = false): bool
In the above syntax of the in_array() function, there are three parameters:
- $needle (required): This parameter represents the value to search for in the array.
- $haystack (required): This parameter is the array in which the function will search for the specified value.
- $strict (optional): If this parameter is set to true, the function will also check the data types of the values, performing a strict comparison (===).
The in_array() function returns true if the value is found. If not found, it returns false value.
Basic Examples of PHP in_array() Function
Example 1: Let’s write a PHP program in which we will check a string exists in an array or not.
<?php
$fruits = ["apple", "banana", "cherry", "orange"];
if (in_array("orange", $fruits)) {
echo "Orange is in the list.";
} else {
echo "Orange is not in the list.";
}
?>
Output: Orange is in the list.
Example 2: Let’s write a PHP program to check a number exists in an array or not.
<?php
$numbers = [10, 20, 30, 40, 50];
if (in_array(30, $numbers)) {
echo "30 is present in the list.";
} else {
echo "30 is not present in the list.";
}
?>
Output: 30 is present in the list.
Using the Strict Parameter
By default (i.e. when set to false), the in_array() function uses loose comparison (==). This can sometimes lead to unexpected results if data types are not considered. Look at the below examples.
Example 3: Loose Comparison (strict = false)
<?php
$values = [1, 2, "3", 4];
if (in_array(3, $values)) {
echo "Value 3 is found without strict.";
}
?>
Output: Value 3 is found without strict.
In this example, the integer value 3 is equal to string value “3” because of using loose comparison.
Example 4: With strict comparison (strict = true)
<?php
$values = [1, 2, "3", 4];
if (in_array(3, $values, true)) {
echo "Value 3 is found without strict.";
} else {
echo "Value 3 is not found with strict.";
}
?>
Output: Value 3 is not found with strict.
In this example, PHP uses strict comparison due to which the integer 3 is not identical to string value “3”.
Checking for Values in Associative Array
The in_array() function only checks values, not keys in an associative array.
Example 5: Associative array
<?php
$person = [
"name" => "Saanvi",
"age" => 25,
"country" => "USA"
];
if (in_array("USA", $person)) {
echo "Country is USA.";
}
?>
Output: Country is USA.
If you want to check for keys, use the array_key_exists() function instead of in_array() function provided by PHP.
Case-insensitive Search with PHP’s in_array() Function
The in_array() function in PHP is case-sensitive by default, which can lead to unexpected results if you’re not aware of this behavior. Let’s take an example to understand case-insensitive searches.
Example 6:
<?php
$colors = ['Red', 'Green', 'Blue', 'Yellow'];
if (in_array('yellow', $colors)) {
echo 'Found yellow!';
} else {
echo 'Yellow not found!'; // This will be executed.
}
?>
Output: Yellow not found!
In this example, the in_array() function returns false because the value ‘yellow’ which is in lowercase, doesn’t match the ‘Yellow’ in the array.
Solutions for Case-Insensitive Search
To make the in_array() function case-insensitive, you can convert both the array values and the search value to lowercase using array_map() and strtolower() before comparison.
Example 7:
<?php
$colors = ['Red', 'Green', 'Blue', 'Yellow'];
// Convert all array values to lowercase.
$lowercaseColors = array_map('strtolower', $colors);
// Search for lowercase 'yellow'.
if(in_array(strtolower('yellow'), $lowercaseColors)) {
echo 'Found yellow!';
} else {
echo 'Yellow not found!';
}
?>
Output: Found yellow!
In this example, the array_map(‘strtolower’, $colors) function converts all values in the array to lowercase. The strtolower(‘yellow’) function also converts the search term into lowercase. Thus, this makes the search case-insensitive.
Searching in Multidimensional Arrays in PHP Using in_array()
The in_array() function in PHP only searches for a value in one-dimensional arrays. If you try to use it directly on a multidimensional array, it won’t work as expected because each element in the outer array is itself an array (or sub-array), not a simple value.
To search within a value from a multidimensional array, you need to loop through each sub-array and use in_array() within the loop. Let’s take an example on it.
Example 8:
<?php
$users = [
["id" => 1, "name" => "Saanvi"],
["id" => 2, "name" => "Tripti"],
["id" => 3, "name" => "Bob"]
];
$searchName = "Tripti";
$found = false;
foreach ($users as $user) {
if (in_array($searchName, $user)) {
$found = true;
break;
}
}
if ($found) {
echo "User '$searchName' found.";
} else {
echo "User '$searchName' not found.";
}
?>
Output: User 'Tripti' found.
In this example, $users is a multidimensional array, which is an array of associative arrays. Then, we have looped over each sub-array. The in_array($searchName, $user) function checks if the value exists in that sub-array. If found, we break out of the loop and set $found to true. After that, we have used if-else statement.
The in_array() function in PHP is used for checking if a value exists in an array. If you have a simple search, use this function. You should use the third parameter (strict) if you want to match data type as well as value. I hope that you will have understood the basic concepts of in_array() function and practiced all examples based on it.