How to use the PHP in_array() Function

In this tutorial, we will be showing you how to use the in_array() function within PHP.

PHP in_array Function

In PHP, the in_array() function allows you to check an array whether a particular value exists within it or not.

Instead of writing a for loop or foreach loop to check if a value exists, you can use this single function. It helps you write more concise code and is great when you don’t need to perform other actions to the array.

The in_array() function works by searching through the array’s values and comparing each value to see if it matches the one you provided.

Over the following few sections, we will explore how the in_array() function is defined within PHP and how to use it.

Table of Contents

Definition of the in_array() Function in PHP

Let us start by exploring how the in_array() function is defined within PHP. It features three different parameters, one of which is optional.

Using these parameters will specify the value you want to search for, the array you want to search for it in, and whether the search should be strict.

Below you can see how the in_array() is defined. This definition shows you the expected variable type and what the function will return.

in_array(mixed $needle, array $haystack, bool $strict = false): bool

Let us now explore each of the three parameters and what they do.

  • $needle – The needle is the value you want to search your array for. This supports multiple data types and can be a string, number, or array.

    If you use a string, then the comparison will be case-sensitive. For example, “Abc” will not be the same as “abc“.
  • $haystack – The haystack is the array that this function will search through. If you have a multi-dimensional array, it will only check the first layer.
  • $stict – The strict value allows you to control how the in_array() function performs its comparisons.

    By default, this is set to “false“, meaning type-juggling will be performed when making comparisons. For most cases, you will want to change this value to “true

In PHP, the in_array() function will return true if it finds the value within the array. If the value is not found, then it will return false.

How to use the in_array() Function in PHP

Now that we understand the basic definition of the in_array() function let us explore the various ways we can use it.

Over the following few sections, we will explore how we can use this function.

Basic Usage of the in_array() Function

We will start our first PHP example of using in_array() by creating an array. We will call this array “$fruits” and store the values "Apple", "Banana", "Cantaloupe", and "Durian" within it.

On our next line, we put the in_array() function to use within a PHP conditional statement.

  • In the first parameter, we pass the value we want to search for, which in our case will be "Apple".
  • For the second parameter, we pass in our “$fruits” array.

If the function finds the "Apple" string, then our condition statement will run and the text “Has Apple” will be echoed.

<?php
$fruits = ["Apple", "Banana", "Cantaloupe", "Durian"];

if (in_array("Apple", $fruits)) {
    echo "Has Apple";
}
?>

After running the code above, you will have the following text output. PHP printed this text because the text "Apple" was found within the “$fruits” array.

Has Apple

in_array() Performs Case Sensitive Searches

One thing you have to make a note of is that if you use a string with the in_array() function in PHP, the search will be case-sensitive.

The easiest way to see this behavior is to utilize the same code from our previous example but adjust it slightly.

The only difference with this code is that the first parameter of the in_array() function was changed from "Apple" to "apple".

<?php
$fruits = ["Apple", "Banana", "Cantaloupe", "Durian"];

if (in_array("apple", $fruits)) { 
    echo "Has Apple";
}
?>

Since PHP will perform the string comparison as case-sensitive, the text "apple" will not match "Apple". Therefore, no text will be printed as PHP’s in_array() will return false.

Using the in_array() Functions Strict Parameter in PHP

For our following example, let us explore how the strict parameter works with PHP’s in_array() function. Using this parameter, we can tell the function that it should perform a strict comparison.

The strict comparison would be the equivalent of using PHP’s identical operator. With “strict” set to true, PHP will only consider values the same if their data types are identical.

Let us start our PHP example by creating an array called “$example_array“. Within this array, we will store the following values, "24", 45, "87", and 45. You will notice that we use different data types.

For our first in_array() function call, we will for an element we know exists.

  • For the “needle” parameter, we will be using the string "24".
  • Next, we set the “haystack” parameter to our “$example_array“. This is the array that we will search for our value in.
  • Finally, we set the “strict” parameter to true. This tells PHP’s in_arrray() function to make strict comparisons during its search.

If the element we are looking for is in this array, we use PHP’s echo statement to print the text “Found the string "24"“.

For our second PHP in_array() function call, we use almost the same parameters. However, this time, we will switch the first parameter from the string "24" to the number 24.

Thanks to the strict comparison, this conditional statement will return false and the string will not be printed.

<?php
$example_array = ["24", 45, "87", 34];

if (in_array("24", $example_array, true)) {
    echo 'Found the string "24"';
}

if (in_array(24, $example_array, true)) {
    echo 'Found the number 24';
}
?>

After running the above code example, you will only end up with one string echoed. The first call to in_array() will return true since the string "24" is in the array.

The second call to in_array() will have returned false since the number 24 is unavailable in the array, and PHP will not perform type juggling since strict is enabled.

Found the string "24"

The in_array() Function Supports Arrays as a Needle

PHP’s in_array() function fully supports using an array as the needle. It will expect that needle to exist within the array.

Let us start this particular example by creating an array that contains arrays. We will name this array “$mult_array” and it will contain the values “['p', 'i']“, “['m', 'y']“, and 'lifeup'.

For our next step we utilize the in_array() function to check if a particular array exists within “$mult_array“.

  • In the needle parameter we pass in the array “['p', 'i']“.

    The function will search the haystack for this value.
  • For our haystack, we will pass in the “$mult_array” array.

    This is the haystack we will search for the needle.

If our needle array does exist within the “$mult_array” array then it will return true and the message “Found p i Array” will be printed.

<?php
$mult_array = [
    ['p', 'i'],
    ['m', 'y'],
    'lifeup'
];

if (in_array(['p', 'i'], $mult_array)) {
    echo "Found p i Array";
}
?>

As the “['p', 'i']” array exists within our “$mult_array” variable, the function will return true and the message “Found p i Array” will be printed.

Found p i Array

Conclusion

Throughout this tutorial, we have shown you how to use the in_array() function in the PHP language.

This function lets you to quickly check if the specified value exists within a chosen array.

Please comment below if you have any questions about using the in_array() function.

To learn more, check out our many other PHP tutorials. We also have other coding guides if you want to learn a different language.

Leave a Reply

Your email address will not be published.