Home Backend Development PHP Problem How to check whether a certain value belongs to an array in php

How to check whether a certain value belongs to an array in php

Apr 26, 2023 am 10:34 AM

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('apple', 'banana', 'orange');
if (in_array('apple', $fruits)) {
    echo 'apple belongs to fruits array';
} else {
    echo 'apple does not belong to fruits array';
}
?>
Copy after login

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('apple', 'banana', 'orange');
$search = array_search('apple', $fruits);
if ($search !== false) {
    echo 'apple belongs to fruits array, and its key is ' . $search;
} else {
    echo 'apple does not belong to fruits array';
}
?>
Copy after login

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('apple', 'banana', 'orange');
if (isset($fruits[array_search('apple', $fruits)])) {
    echo 'apple belongs to fruits array';
} else {
    echo 'apple does not belong to fruits array';
}
?>
Copy after login

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 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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

What are the best practices for deduplication of PHP arrays

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

What Are the Latest PHP Coding Standards and Best Practices?

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

Can PHP array deduplication take advantage of key name uniqueness?

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

How Do I Work with PHP Extensions and PECL?

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

How to Implement message queues (RabbitMQ, Redis) in PHP?

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

Does PHP array deduplication need to be considered for performance losses?

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

How to Use Reflection to Analyze and Manipulate PHP Code?

See all articles