0
0
PHPprogramming~5 mins

Array search functions in PHP

Choose your learning style9 modes available
Introduction

Array search functions help you find if a value exists in a list and where it is. This saves time compared to checking each item yourself.

You want to check if a name is in a list of attendees.
You need to find the position of a product ID in an inventory array.
You want to verify if a user input matches any value in a predefined list.
You want to find the key of a specific value in an associative array.
You want to check if a value exists before adding it to avoid duplicates.
Syntax
PHP
<?php
// Search for a value in an array and get its key
$key = array_search(mixed $needle, array $haystack, bool $strict = false);

// Check if a value exists in an array
$exists = in_array(mixed $needle, array $haystack, bool $strict = false);
?>

array_search returns the key of the found value or false if not found.

in_array returns true if the value exists, otherwise false.

Examples
Finds the key of 'banana' in the array. Keys start at 0.
PHP
<?php
$fruits = ['apple', 'banana', 'cherry'];
$key = array_search('banana', $fruits);
echo $key; // Outputs 1
?>
Checks if 'grape' is in the array. It is not, so it prints 'Not found'.
PHP
<?php
$fruits = ['apple', 'banana', 'cherry'];
$exists = in_array('grape', $fruits);
echo $exists ? 'Found' : 'Not found'; // Outputs 'Not found'
?>
Searching in an empty array returns false.
PHP
<?php
$emptyArray = [];
$key = array_search('anything', $emptyArray);
var_dump($key); // Outputs bool(false)
?>
Searching for the only element returns key 0.
PHP
<?php
$singleElement = ['only'];
$key = array_search('only', $singleElement);
echo $key; // Outputs 0
?>
Sample Program

This program shows how to use array_search and in_array to find values in arrays. It prints the array, searches for values, and handles empty arrays.

PHP
<?php
// Define an array of colors
$colors = ['red', 'green', 'blue', 'yellow'];

// Print the original array
echo "Original array:\n";
print_r($colors);

// Search for 'blue' in the array
$searchKey = array_search('blue', $colors);
if ($searchKey !== false) {
    echo "\nFound 'blue' at key: $searchKey\n";
} else {
    echo "\n'blue' not found in the array.\n";
}

// Check if 'purple' exists in the array
$exists = in_array('purple', $colors);
echo $exists ? "\n'purple' is in the array.\n" : "\n'purple' is not in the array.\n";

// Search for 'red' in an empty array
$emptyArray = [];
$emptySearch = array_search('red', $emptyArray);
echo $emptySearch === false ? "\n'red' not found in empty array.\n" : "\nFound 'red' in empty array.\n";
?>
OutputSuccess
Important Notes

Time complexity: Both array_search and in_array check items one by one, so they take time proportional to the array size (O(n)).

Space complexity: These functions do not use extra space beyond a few variables.

A common mistake is to check if (array_search(...) == false) which fails if the found key is 0. Always use strict comparison !== false.

Use array_search when you need the key of the found value. Use in_array when you only need to know if the value exists.

Summary

Array search functions help find values or keys in arrays easily.

array_search returns the key or false if not found.

in_array returns true or false depending on presence.