PHP IN_ARRAY Function: How To Find a Value in an Array
The PHP IN_ARRAY function is a function used to determine whether a given value is within an array. It returns true if the value is found; it returns false if the value isn’t found. While it does seem to be a simple function, it’s actually very useful.
Let’s take a deeper look at the IN_ARRAY function, when it returns true or false, and why you might need a function that checks for a value in an array.
The syntax and usage of the PHP IN_ARRAY function
The PHP IN_ARRAY function begins with the following syntax:
in_array($needle, $haystack, $strict);
In the above example, the variables are as follows:
- $needle: the value that is being searched for.
- $haystack: the array that is being searched.
- $strict: if it’s a strict type.
$strict bears some explanation. Strict typing refers to whether the value is an int, string, etc. If the “strict” is set to true, then a string “1” won’t match with an int “1.” If the strict is not true, then they will match.
Last Updated October 2022
PHP for Beginners: learn everything you need to become a professional PHP developer with practical exercises & projects. | By Edwin Diaz, Coding Faculty Solutions
Explore CourseAn example of the IN_ARRAY function
As an example of the IN_ARRAY function, we will create an array that includes four values: Apples, Oranges, Pears, and Grapes.
<?php
$example[0] = 'Apples';
$example[1] = 'Oranges';
$example[2] = 'Pears';
$example[3] = 'Grapes';
if(in_array('Apples', $example)) {
print("We've got apples!<br>");
}
if(!in_array('Bananas', $example)) {
print("No, we've got no bananas!");
}
?>
In the above example, we first create the array. Then, we check to see if there are apples. Then, we check to see if there are bananas.
So, we can see that the first test in_array(‘Apples’,$example) returns a “true” because “Apples” exists in an array. We see the second test is prefaced by a “!” because that means not. We’re testing if in_array(‘Bananas’,$example) fails. It does, so we know we don’t have “Bananas.”
Strict typing with the IN_ARRAY function
Strict mode and strict typing will check the types as well as the values. Now, due to PHP not having strict typing by default, this is actually a little confusing. Let’s take a look at an example.
<?php
$example[0] = 1;
$example[1] = 2;
$example[2] = 3;
$example[3] = '4';
if(in_array(1, $example)) {
print("There's a 1 in this example.<br>");
}
if(in_array(4, $example)) {
print("There's a 4 in this example.");
}
?>
In the above example, we have an array that includes 1, 2, 3, and ‘4.’ The quotes matter: 4 isn’t actually an int: it’s a string. But we also have strict typing turned off because we have not assigned it a value. Here, we aren’t looking to check the types.
So, the array to search returns “yes” each time. But let’s turn strict typing on.
It’s as we expected. With strict typing on, the 4 isn’t found, because the 4 isn’t an int. Now, if we change what we’re looking for to a string, it will work:
Most developers won’t need to turn on strict typing when looking for a needle in the haystack, but it’s important to remember that the array returns non-strict typing by default, and strict typing has to be turned on manually.
Top courses in PHP
Case sensitivity with IN_ARRAY
Now, it should be noted that in addition to not turning on strict typing, the PHP IN_ARRAY function defaults to case sensitive. Let’s take a look at the following code:
<?php
$example[0] = 'Blue';
$example[1] = 'Green';
$example[2] = 'Red';
$example[3] = 'White';
if(in_array('blue', $example)) {
print("There's a blue in this array.<br>");
} else echo "We couldn't find blue.";
?>
What happens when we run this code?
And that makes sense because “blue” isn’t in the array. Blue is. So, what do we do if we want to run IN_ARRAY in a case-insensitive fashion? Unfortunately, we actually need to change the cases of everything. We can do this using the STRTOLOWER() function, which will reduce the case of a string to lowercase, or the STRTOUPPER() function, which will increase the case of each string to uppercase.
Technically, we could also use PHP STR_REPLACE to replace any uppercase values with lowercase values and vice versa, but STRTOLOWER and STRUPPER are the shorthand versions of this.
PHP students also learn
IN_ARRAY and multidimensional arrays
In PHP, you can create multi-dimensional arrays. We’re going to create one of these now and test it out with IN_ARRAY.
$example[0]['First_Name'] = 'John';
$example[0]['Last_Name'] = 'Doe';
$example[1]['First_Name'] = 'Mary';
$example[1]['Last_Name'] = 'Smith';
if(in_array("Mary",$example)) {
print("Mary is a customer here.");
}
if(in_array("Eleanor",$example)) {
print("Eleanor is a customer here.");
}
If you run this example, you’ll see that, unfortunately, it prints nothing. We would have expected Mary to be found and not Eleanor, but neither are found because of how the array has been constructed.
What do we need to do? Well, we’d have to add [1] — we need to search the specific array. We can only search one dimension deep.
This does give us the right result, but note that $example[0] is never searched. We can only search one level of the array at a time — which means we would need to iterate using FOREACH if we wanted to search for John as well.
Using IN_ARRAY in your PHP code
Now, we’ve seen how an array function searches for a specific value within it using IN_ARRAY. With IN_ARRAY, the array returns true if the value is found and false if the value isn’t. It’s a case-sensitive, non-typed function — and it’s a pretty useful one.
In PHP, arrays are both easy and challenging. They are easy because it’s straightforward to create and manipulate an array. They are challenging because they are not strictly typed and very malleable. For instance, PHP is one of the very few languages in which a single array can hold strings, ints, and other value types.
As you move forward, you might want to learn more about what PHP is, how it’s used, and how PHP functions are developed. PHP IN_ARRAY is only one of many useful functions built into the platform.