Using in_array vs array_search: 3 PHP Code Examples (2023)

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

PHP in_array and array_search are two functions that seem to have a common function, that is, to search an array for a value. However, they do this function in different ways, and they have different return types. We have already seen the array_search in-depth. We recommend you to go through that article to have some insights about the function.

In a nutshell, the array_search looks for a value in a PHP array and returns the corresponding key. In comparison, the in_array function looks for a value and returns true if it exists. This article compares the two functions and highlights the difference between in_array and array_search.

What is the difference between in_array and array_search?

This section gives a review of the two functions and elaborates the differences between in_array and array_search. After the reviews and examples, you would clearly understand the difference between these PHP functions and be more aware of their use.

in_array review

Here’s a brief overview of the PHP in_array function.

Function Signature

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

Description

The function takes two arguments, needle and haystack. The needle refers to the value the function should look for in the haystack, the input array. A third argument strict indicates a strict comparison between the needle and the array values. A strict comparison is comparing values as well as the data types.

in_array and array_search

Return Type

PHP in_array returns true if the value exists in the array, else false.

PHP in_array Code Examples

The example uses PHP in_array to find some values in the array.

<?php
$names_arr = [
    "Irwin",
    "Josh",
    "Marry",
    "Rahul",
    "Jack",
    "Tobey"
];
 
//Returns true.
if(in_array("Tobey", $names_arr)) {
    echo "Tobey present".PHP_EOL;
}
 
//Returns true.
if(in_array("Jack", $names_arr)) {
    echo "Jack present".PHP_EOL;
}
 
//Returns false (Case sensitive)
if(in_array("marry", $names_arr)) {
    echo "Marry present".PHP_EOL;
}
 
//Returns false
if(in_array("John", $names_arr)) {
    echo "John present".PHP_EOL;
}
 
//OUTPUT
//Tobey present
//Jack present
 
?>

Voila! The example is self-explanatory. Just remember that string comparisons are case-sensitive, and that’s why it returns false for “marry.” 

The following example shows the function with the strict set to true.

<?php
$numbers = [3.14159, 2.012, 500];
 
//Non-strict comparison
if(in_array("3.14159", $numbers)) {
    echo "3.14159 exists".PHP_EOL;
}
 
//Strict comparison
if(in_array("500", $numbers, true)) {
    echo "500 exists".PHP_EOL;
}
 
//OUTPUT
//3.14159 exists
 
?>

In the non-strict comparison, the function just looks for the value and ignores that the needle is a string value while we have a numbers array. Contrary to this, the strict comparison checks both value and data type.

The in_array function can find arrays too. Check out the following example.

<?php
$arr = [array('A', 'B'), array('C', 'D'), 'E'];
 
 
//Returns True.
if(in_array(['A','B'], $arr)) {
    echo "[A, B] exists".PHP_EOL;
}
 
//Returns False.
if(in_array(['G','H'], $arr)) {
    echo "[G, H] exists".PHP_EOL;
}
 
//OUTPUT
//[A, B] exists
 
?>

The example is intuitive enough. All the examples have demonstrated the PHP in_array function and set a solid ground for comparing this function with the array_search.

array_search review

Here’s a brief overview of the PHP array_search function.

Function Signature

array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false

Description

The array_search function takes two arguments, needle and haystack. Much like the in_array function, the array_search looks for a value. The comparison behavior depends on the strict argument. However, this function returns the corresponding key of the first match. One important caveat is that it ignores the other keys if there is more than a single match.

in_array and array_search

Return Type

This function returns the matched key for the needle, or it returns false.

PHP array_search Code Examples

The example uses the PHP array_search to find the value and return the corresponding key.

<?php
$developers = [
    "Anna" => "Javascript",
    "Edward" => "Java",
    "Rey" => "Python",
    "Sanjay" => "PHP",
    "Ali" => "C++",
    "Sarah" => "C#",
    "Bob" => "Android"
];
 
print_r(array_search("PHP", $developers));
 
//OUTPUT
//Sanjay
?>

So, the example uses an array with developers’ names as keys and their preferred skills as values. The needle in the example is “PHP,” and the haystack is the array. PHP array_search looks for a match in the array values and returns the corresponding key, “Sanjay.”

The PHP array_search will always return the corresponding key of the first match, ignoring all the other matches if they exist. The array_search article has some excellent examples that are a workaround for this function’s limitation.

Conclusion

This article explores the difference between in_array and array_search. In summary, these functions are look-alikes, but they differ in their return type. The in_array returns a boolean while the array_search returns a key. They can be used interchangeably. The article provides an overview of these functions and we hope you’ve learned the difference between these two functions. Stay tuned for more interesting articles and tutorials related to PHP.

Want to explore more useful PHP tutorials?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

How to merge 2 arrays with the same keys in PHP

Difference between array_combine and array_merge in PHP

PHP array_search: How to use in PHP with examples

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.