Open In App

Search an Item in an Array in PHP

Last Updated : 15 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array and an item, the task is to search whether the given item is present in the array or not.

Below are the approaches to search an item in an array in PHP:

Using in_array() Function

The in_array() function checks if a value exists in an array. It returns true if the value is found, and false otherwise.

Example: The in_array() function is very basic and checks if the value 30 exists in the array.

PHP
<?php

$arr = array(10, 20, 30, 40, 50);

$item = 30;

if (in_array($item, $arr)) {
    echo "$item exist in array.";
} else {
    echo "$item not exist in array.";
}

?>

Output
30 exist in array.

Using array_search() Function

The array_search() function searches for a value in an array and returns the key if found. If the value is not found, it returns false.

Example: The example shows the use of array_search() function.

PHP
<?php

$arr = array(10, 20, 30, 40, 50);

$item = 30;

$key = array_search($item, $arr);

if ($key !== false) {
    echo "$item exist in array.";
} else {
    echo "$item not exist in array.";
}

?>

Output
30 exist in array.

Using array_key_exists() Function

If you want to search for a key in an associative array, array_key_exists() is the right function to use. It checks if the given key or index exists in the array. The array_key_exists() function checks if the key "C" exists in the associative array.

Example: This example shows the use of array-key_exists() function for searching an item in an array.

PHP
<?php

$arr = array(
    'A' => 10, 
    'B' => 20, 
    'C' => 30, 
    'D' => 40, 
    'E' => 50
);

$itemKey = 'C';

if (array_key_exists($itemKey, $arr)) {
    echo "$itemKey exist in array.";
} else {
    echo "$itemKey not exist in array.";
}

?>

Output
C exist in array.

Using a Loop

If you need to perform a more complex search, you can use a loop to iterate through the array and check each element. The loop iterates through the array and compares each element with the search value 30. If a match is found, it prints the index and breaks the loop.

Example: This example shows the use of loop for searching an element in an array.

PHP
<?php

$arr = array(10, 20, 30, 40, 50);
$item = 30;
$found = false;

foreach ($arr as $key => $value) {
    if ($value == $item) {
        echo "$item Exist in Array at Index $key.";
        $found = true;
        break;
    }
}

if (!$found) {
    echo "$item not Exist in Array.";
}

?>

Output
30 Exist in Array at Index 2.

Using array_filter() with count()

In this approach we use array_filter() to create a new array containing elements that match the search value and then checks if the filtered array has any elements using count(). If the count is greater than zero, the item exists in the original array.

Example: This example shows the use of array_filter() with count() method to search an element in an array.

PHP
<?php
$array = array('apple', 'banana', 'cherry');
$search = 'banana';

$filtered_array = array_filter($array, function($item) use ($search) {
    return $item == $search;
});

if (count($filtered_array) > 0) {
    echo "$search found in the array.";
} else {
    echo "$search not found in the array.";
}
?>

Output
banana found in the array.

Next Article

Similar Reads