Home > Article > Backend Development > 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:
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!