PHP determines whether an array contains

WBOY
Release: 2023-05-05 18:30:01
Original
1111 people have browsed it

php determines whether an array contains

In PHP development, it is often necessary to determine whether an array contains a specific element, such as determining whether an order list contains a certain product. This article will introduce several commonly used PHP methods to determine whether an array is included.

1. Use the in_array() function

The in_array() function is a built-in function in PHP, used to determine whether an element exists in an array. The syntax format of this function is as follows:

bool in_array(mixed $needle, array $haystack [, bool $strict = FALSE ])

Among them, $needle is the element to be found, $haystack is the array being searched, and $strict is an optional parameter. If set to TRUE, it will not only determine whether the values of the elements are equal, but also determine whether the data types of the elements are equal.

The sample code is as follows:

Copy after login

The output result is: There is Apple

2. Use the array_search() function

array_search() function is also built-in in PHP Function that finds the key of a value in an array and returns that key. If the value does not exist, returns false. The syntax format of this function is as follows:

mixed array_search(mixed $needle, array $haystack [, bool $strict = false ])

Among them, $needle is the element to be found, $haystack is the array being searched, and $strict is an optional parameter. If set to TRUE, it will not only determine whether the values of the elements are equal, but also determine whether the data types of the elements are equal.

The sample code is as follows:

Copy after login

The output result is: there is apple, the key value is: 0

3. Use isset() function

isset( ) function is used to determine whether a variable exists and the value is not null. This function can be used to determine whether an element exists in an array. The sample code is as follows:

Copy after login

The output result is: There is Apple

It should be noted that if the value of an element is null, the isset() function will return false. Therefore, the isset() function cannot be used to determine whether the value of an element is null.

4. Use the array_key_exists() function

The array_key_exists() function is used to determine whether a key name exists in the array. The syntax format of this function is as follows:

bool array_key_exists(mixed $key, array $array)

Among them, $key is the key name to be searched, and $array is the array to be searched.

The sample code is as follows:

 '红苹果', 'banana' => '香蕉', 'orange' => '柑橘'); if (array_key_exists('apple', $fruits)) { echo "有苹果,值为:" . $fruits['apple']; } else { echo "没有苹果"; } ?>
Copy after login

The output result is: there are apples, the value is: red apples

It should be noted that the array_key_exists() function can only be used to find keys Whether the name exists cannot be used to find whether the key value exists.

To sum up, the above four methods can be used to determine whether an array contains a specific element. Developers can choose which method to use according to the actual situation in order to better realize the program functions.

The above is the detailed content of PHP determines whether an array contains. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
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!