Home  >  Article  >  Backend Development  >  How to Check if an Array Contains a Specific Value in PHP?

How to Check if an Array Contains a Specific Value in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-11-11 11:43:021004browse

How to Check if an Array Contains a Specific Value in PHP?

Checking Array Value Existence in PHP

In PHP, determining if an array contains a specific value is a common task. To achieve this, the recommended solution is to utilize the built-in in_array() function.

in_array() takes two parameters:

  1. The value to search for
  2. The array to search within

If the value is found within the array, the function returns true, indicating its presence. Otherwise, it returns false.

Here's an example:

$array = ['kitchen', 'bedroom', 'living_room', 'dining_room'];

if (in_array('kitchen', $array)) {
    echo 'this array contains kitchen';
}

In this example, the in_array() function checks if the array contains the value 'kitchen'. If it does, it prints "this array contains kitchen." Otherwise, it does not print anything.

The above is the detailed content of How to Check if an Array Contains a Specific Value in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn