search
HomeBackend DevelopmentPHP ProblemHow to check whether a certain value belongs to an array in php

In PHP, querying whether a value belongs to an array is a very basic operation. This article will introduce three different methods to determine whether a value belongs to an array.

First method: use the in_array() function

PHP provides an in_array() function, which can be used to determine whether a value belongs to an array. This function has two parameters: the first parameter is the value to be queried, and the second parameter is the array to be queried. The function will return true if the first argument belongs to the second argument, false otherwise.

The following is an example:

<?php $fruits = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);
if (in_array(&#39;apple&#39;, $fruits)) {
    echo &#39;apple belongs to fruits array&#39;;
} else {
    echo &#39;apple does not belong to fruits array&#39;;
}
?>

The above code will output: apple belongs to fruits array

Second method: use array_search() function

PHP also provides another function array_search(), which can be used to query whether a value belongs to an array, and this function can also return the position of the value in the array. If the query is successful, this function will return the key name of the value in the array, otherwise it will return false.

The following is an example:

<?php $fruits = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);
$search = array_search(&#39;apple&#39;, $fruits);
if ($search !== false) {
    echo &#39;apple belongs to fruits array, and its key is &#39; . $search;
} else {
    echo &#39;apple does not belong to fruits array&#39;;
}
?>

The above code will output: apple belongs to fruits array, and its key is 0

Please note that if false is found in the array , 0 or '', will also return false, so we need to use the identity operator (===) to distinguish between returning false and returning 0.

Third method: Use isset() function

isset() function is used to determine whether a variable exists and whether the variable has a value set. Because in an array, whether a certain key has been set to a certain value is often the result of whether we want the query. Therefore, we can also use the isset() function to determine whether a value belongs to an array. The isset() function will return true if the variable has been set to a certain value, otherwise it will return false.

The following is an example:

<?php $fruits = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);
if (isset($fruits[array_search(&#39;apple&#39;, $fruits)])) {
    echo &#39;apple belongs to fruits array&#39;;
} else {
    echo &#39;apple does not belong to fruits array&#39;;
}
?>

The above code will output: apple belongs to fruits array

Please note that since the array_search() function is used in this example, We have judged the results to avoid certain unforeseen errors.

The above is the detailed content of How to check whether a certain value belongs to an array 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.