PHP in_array function simplified
In this short tutorial we will be learning about thein_array()
function in PHP. We will further look into its syntax, usage and examples as well.Table of Contents
- What is in_array in PHP?
- Syntax and parameters
- Using the function in non-strict mode
- Using the function in strict mode
- The in_array() function with the searched value being an array
- Closing thoughts
- Other Related Concepts
What is in_array in PHP?
This function in PHP,in_array()
, searches for an existing value in an array. This is a built-in function in PHP. It returns True if the value is found in the array, otherwise it returns False.In the parameters of this function, if the search parameter is a string and the type parameter is set to
TRUE
, the search would be case-sensitive. To understand this concept better, it’s important to understand the syntax of this function. Syntax of the PHP implode function
Here is the syntax of the in_array function:in_array(search, array, type)
So what do these mentioned parameters mean? Let’s take a look -
- Search: This is a required value for this function to work. This parameter can be of mixed type i.e, it can be of string type or integer type, or any other type. If this parameter is of string type then the search will be performed in a case-sensitive manner.
- Array: This is another required parameter for this function to work. It specifies the array we want to search in.
- Type: This is an optional parameter that can be included in this function. If it is set to
TRUE
, then thein_array()
function searches for the value with the same type of value as specified by the search parameter. The default value of this parameter isFALSE
.
Return value: The function in_array()
returns a Boolean value. The possible values are TRUE
, if the search value is found in the array and FALSE
if it isn’t.
Using the in_array function in the non-strict mode
In this section, we are going to look at an example program that performs the search in the non-strict mode. This means that the last parameter of the function is going to be set toFALSE
. This Boolean value is the function’s default value. Input:
?php
$age_group = array(45, 57, 49, 44);
if (in_array(“57”, $age_group))
{
echo “Available”;
}
else
{
echo “Unavailable”;
}
?>
Output:
Available
The value put in the search parameter, by default, is of string type. But since the function
in_array()
is in non-strict mode, the function returns the value as TRUE
despite the searched value being an integer. Using the function in strict mode
Now, let us look at another example, but this time let’s use the function in its strict mode. So in this case, the last parameter of the function will be set toTRUE
. Once this is done, the function will also check for the type of the value searched. Input:
?php
$companies = array("flexiple", "toptal", "arc.dev", 1, 67, 2);
if (in_array("flexiple", $companies, TRUE))
{
echo "Available \n";
}
else
{
echo "Unavailable \n";
}
if (in_array(1, $companies, TRUE))
{
echo "Available \n";
}
else
{
echo "Unavailable \n";
}
if (in_array("3", $companies, TRUE))
{
echo "Available \n";
}
else
{
echo "Unavailable \n";
}
?>
Output:
Available
Available
Unavailable
Using in_array() function with the searched value being an array
For further understanding, let’s use the same function with the value being searched as an array. Let’s see what the code and output is like in such an instance.Input:
?php
$companies = [
['tech mahindra', 'infosys', 'tcs'],
['cognizant', 'accenture', 'capgemini'],
['skelia', 'plaxonic']
];
if (in_array(['tech mahindra', 'infosys', 'tcs'], $companies)) {
echo 'Indian tech firms found';
}
else
{
echo 'Indian tech firms not found';
}
Output:
Indian tech firms found
Closing thoughts
In this short tutorial we have seen how to check if a value exists in an array or not. Similar to how we had the search parameter itself as an array, we can also search with the parameter being an array of objects. In such a case, if the type parameter is set toTRUE
, the function will compare objects using their identities instead of values.