Home > Backend Development > PHP Problem > Does php array value exist?

Does php array value exist?

WBOY
Release: 2023-05-19 11:03:37
Original
509 people have browsed it

In PHP programming, operations on arrays are often involved, and querying whether a certain value exists in an array is also a common requirement. PHP provides a variety of methods to accomplish this operation, and this article will introduce these methods one by one.

  1. in_array function

The in_array function is one of the commonly used functions in PHP to query whether a value exists in an array. The syntax of this function is as follows:

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

Among them, $needle is the value to be queried, $haystack is the array to be queried, $strict is an optional parameter, indicating whether to use strict mode when comparing (that is, the types are different are not equal, the default is false).

The following is an example:

$arr = array('apple', 'banana', 'orange');
if(in_array('apple', $arr)){
    echo '数组中存在apple';
}else{
    echo '数组中不存在apple';
}
Copy after login

This code will output "apple exists in the array".

  1. array_search function

The array_search function is another way to query whether a certain value exists in an array. The syntax of this function is as follows:

mixed array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Copy after login

Among them, $needle is the value to be queried, $haystack is the array to be queried, $strict is an optional parameter, indicating whether to use strict mode when comparing (that is, the types are different are not equal, the default is false). 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:

$arr = array('apple', 'banana', 'orange');
$index = array_search('orange', $arr);
if($index === false){
    echo '数组中不存在orange';
}else{
    echo '数组中存在orange,键名为' . $index;
}
Copy after login

This code will output "orange exists in the array, the key name is 2".

It should be noted that if there are multiple values ​​in the array, only the key name of one of them will be returned.

  1. isset function

isset function is a function used in PHP to determine whether a variable exists. In an array, we can use the isset function to determine whether a key exists. The following is an example:

$arr = array('apple', 'banana', 'orange');
if(isset($arr[1])){
    echo '数组中存在键名为1的元素';
}else{
    echo '数组中不存在键名为1的元素';
}
Copy after login

This code will output "There is an element with the key name 1 in the array."

It should be noted that when using the isset function to determine the key name that does not exist in the array, no error will be reported, only false will be returned.

  1. array_key_exists function

The array_key_exists function is a function used in PHP to determine whether a key name exists in an array. The syntax of this function is as follows:

bool array_key_exists ( mixed $key , array $array )
Copy after login

Among them, $key is the key name to be queried, and $array is the array being queried. If the query is successful, this function will return true, otherwise it will return false.

The following is an example:

$arr = array('apple', 'banana', 'orange');
if(array_key_exists(2, $arr)){
    echo '数组中存在键名为2的元素';
}else{
    echo '数组中不存在键名为2的元素';
}
Copy after login

This code will output "There is an element with key name 2 in the array".

It should be noted that when using the array_key_exists function to determine the key name that does not exist in the array, no error will be reported, only false will be returned.

To sum up, PHP provides a variety of methods to query whether a certain value or key name exists in an array. Developers can choose the most suitable method based on specific needs.

The above is the detailed content of Does php array value exist?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template