How to Search an Array for a Value in PHP

09/06/2021

Contents

In this article, you will learn how to search an array for a value in PHP.

PHP array_search() Function

The array_search() function is used to search for a value in an array and return its key if found. If the value is not found, array_search() returns false.

Here’s an example:

<?php
  $array = array("apple", "banana", "cherry");
  $search_value = "banana";

  $key = array_search($search_value, $array);

  if ($key !== false) {
    echo "Value found. Key: " . $key;
  } else {
    echo "Value not found.";
  }
?>

In this example, the value “banana” is found in the array and its key, 1, is returned. If the value had not been found, array_search() would have returned false.

You can also use the $strict parameter with array_search() to specify if type matching should be used during the search:

<?php
  $array = array(1, "1", 2, 3);
  $search_value = "1";

  $key = array_search($search_value, $array, true);

  if ($key !== false) {
    echo "Value found (strict mode). Key: " . $key;
  } else {
    echo "Value not found (strict mode).";
  }

  $key = array_search($search_value, $array, false);

  if ($key !== false) {
    echo "Value found (non-strict mode). Key: " . $key;
  } else {
    echo "Value not found (non-strict mode).";
  }
?>

Here are a few additional details and tips regarding the array_search() function in PHP:

  • When using array_search(), it is important to check the returned value against false using the !== operator, as a value of 0 or null might also be present in the array.
  • If multiple values in the array match the search value, array_search() returns the key of the first match it finds.
  • If you need to search for multiple values in an array, it might be more efficient to use array_keys() to get all the keys for a given value and then use those keys to access the values in the array.

Here’s an example using array_keys():

<?php
  $array = array("apple", "banana", "cherry", "banana");
  $search_value = "banana";

  $keys = array_keys($array, $search_value);

  if (!empty($keys)) {
    echo "Values found. Keys: " . implode(", ", $keys);
  } else {
    echo "Values not found.";
  }
?>

In this example, the values “banana” are found in the array with keys 1 and 3, which are returned in an array by array_keys(). The implode() function is used to join the keys into a comma-separated string for printing.

PHP in_array() Function

You can use the in_array() function to search an array for a value in PHP:

<?php
  $array = array("apple", "banana", "cherry");
  $search_value = "banana";

  if (in_array($search_value, $array)) {
    echo "Value found.";
  } else {
    echo "Value not found.";
  }
?>

The in_array() function returns true if the value is found in the array and false if not.

Here are some additional details and examples to help you better understand the in_array() function in PHP:

  • The in_array() function searches for a value in a one-dimensional array and returns a Boolean value indicating if the value was found or not.
  • You can also specify a third parameter, $strict, which determines if type matching should be used during the search. By default, $strict is set to false, meaning type coercion will be performed. If $strict is set to true, then the types must match exactly.

Here’s an example using the $strict parameter:

<?php
  $array = array(1, "1", 2, 3);
  $search_value = "1";

  if (in_array($search_value, $array, true)) {
    echo "Value found (strict mode).";
  } else {
    echo "Value not found (strict mode).";
  }

  if (in_array($search_value, $array, false)) {
    echo "Value found (non-strict mode).";
  } else {
    echo "Value not found (non-strict mode).";
  }
?>

In this example, when $strict is set to true, the value “1” is not found in the array because it is a string, while the values in the array are integers. But when $strict is set to false, the value is found because type coercion is performed.