Home  >  Article  >  Backend Development  >  PHP determines whether it exists in an array

PHP determines whether it exists in an array

WBOY
WBOYOriginal
2023-05-06 11:53:07457browse

In PHP development, we often encounter situations where we need to determine whether an element exists in an array. In this case, we usually use the in_array() function or array_search() function. Below we will introduce the usage and differences of these two functions in detail.

1. in_array() function

The in_array() function is a function commonly used in PHP to determine whether an element exists in an array. The method of using it is as follows:

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

Among them, $needle represents the element to be found, $haystack represents the array to be found, and $strict represents whether to compare congruently. Its return value is a Boolean value.

The sample code is as follows:

$fruits = array("apple", "banana", "orange", "tomato");

if (in_array("apple", $fruits)) {
    echo "有苹果!";
} else {
    echo "没有苹果。";
}

The output result is "There are apples!".

2. array_search() function

The array_search() function has a similar function to the in_array() function, and can also be used to find whether an element exists in an array. Unlike the in_array() function, the array_search() function returns the subscript of the element or false.

The array_search() function is used as follows:

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

Among them, $needle represents the The element to be searched for, $haystack represents the array to be searched, and $strict represents whether to compare congruently.

The sample code is as follows:

$fruits = array("apple", "banana", "orange", "tomato");

$key = array_search("orange", $fruits);

if ($key !== false) {
    echo "'orange' 的下标是: " . $key;
} else {
    echo "'orange' 不存在!";
}

The output result is "The subscript of 'orange' is: 2".

3. The difference between the in_array() function and the array_search() function

Although the in_array() function and the array_search() function have similar functions in determining whether an element exists in an array, they The two still have the following differences:

  1. The return values ​​are different: in_array() returns a Boolean value, array_search() returns the subscript of the element or false.
  2. The comparison methods are different: the in_array() function only compares the value of the element, and the array_search() function can compare both the key and the value of the element.
  3. Usage restrictions are different: the array_search() function can only be used for one-dimensional arrays, while the in_array() function can be used for multi-dimensional arrays.

4. Multiple ways to determine whether an element exists in an array

In addition to the in_array() function and array_search() function, there are other ways to determine whether an element exists in an array. , such as using the isset() function and array_key_exists() function.

  1. Use isset() function

isset() function can be used to determine whether an element exists in an array. The usage method is as follows:

$fruits = array("apple", "banana", "orange", "tomato");

if (isset($fruits[2])) {
    echo "'orange' 存在!";
} else {
    echo "'orange' 不存在!";
}

The output result is "'orange' exists!".

  1. Use array_key_exists() function

array_key_exists() function can also be used to determine whether an element exists in an array. The usage method is as follows:

$fruits = array("apple", "banana", "orange", "tomato");

if (array_key_exists(2, $fruits)) {
    echo "'orange' 存在!";
} else {
    echo "'orange' 不存在!";
}

The output result is "'orange' exists!".

Unlike the isset() function, which can only be used to determine whether the value of the array exists or is empty, the array_key_exists() function can be used to determine whether the key of the array exists.

Conclusion

Through the above introduction, we can see that in PHP development, it is not difficult to determine whether an element exists in an array. We can use the in_array() function, array_search( ) function, isset() function or array_key_exists() function, etc. to achieve this. Which method to use specifically, we need to choose different methods according to different situations to achieve the best results.

The above is the detailed content of PHP determines whether it exists in an array. 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
Previous article:php7 escaped stringNext article:php7 escaped string