


How to check whether a certain value belongs to an array in php
Apr 26, 2023 am 10:34 AMIn 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'; } ?>
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'; } ?>
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'; } ?>
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the best practices for deduplication of PHP arrays

What Are the Latest PHP Coding Standards and Best Practices?

Can PHP array deduplication take advantage of key name uniqueness?

How Do I Work with PHP Extensions and PECL?

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

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

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code?
