How to check if a certain value exists in an array in PHP

PHPz
Release: 2024-03-19 11:52:01
forward
799 people have browsed it

php editor Baicao teaches you how to check whether a certain value exists in an array. In PHP, you can use the in_array() function to determine whether an array contains a specified value. This function accepts two parameters, the first parameter is the value to be found, and the second parameter is the array to be found. Returns true if the specified value is found, false otherwise. Using this function can quickly and easily check whether a certain value exists in the array, making your code more efficient and concise.

How to check whether a certain value exists in an array in PHP

In php, checking whether a value exists in an array is a common task. There are several ways to achieve this:

1. Use in_array() function

grammar:

in_array($value, $array, $strict = false)
Copy after login
  • $value: The value to find.
  • $array: The array to search.
  • $strict (optional): Specify whether to perform strict comparison (case and type sensitive).

Example:

$arr ​​= array("apple", "banana", "cherry");

// Check if "banana" exists in the array
if (in_array("banana", $arr)) {
echo "exist";
} else {
echo "does not exist";
}
Copy after login

2. Use array_key_exists() function

grammar:

array_key_exists($key, $array)
Copy after login
  • $key: The key to search for.
  • $array: The array to search.

Example:

$arr ​​= array("fruit" => "apple", "color" => "red");

// Check if the "fruit" key exists in the array
if (array_key_exists("fruit", $arr)) {
echo "exist";
} else {
echo "does not exist";
}
Copy after login

3. Use isset() function

grammar:

isset($array[$key])
Copy after login
  • $array: The array to search.
  • $key: The key to search for.

Example:

$arr ​​= array("fruit" => "apple", "color" => "red");

// Check if the "fruit" key exists in the array and has been assigned a value
if (isset($arr["fruit"])) {
echo "exist";
} else {
echo "does not exist";
}
Copy after login

Choose the appropriate method

Which method to choose depends on the specific situation:

  • in_array(): Case and type sensitive when values ​​need to be compared.
  • array_key_exists(): When it is necessary to check whether a specific key exists.
  • isset(): When it is necessary to check whether the key exists and has been assigned a value.

Precautions

  • These methods distinguish variable types. If you want to do a type-insensitive comparison, you can use the === or !== operators.
  • For large arrays, in_array() may be slower than array_key_exists() and isset().

The above is the detailed content of How to check if a certain value exists in an array in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!