Home  >  Article  >  Backend Development  >  php determines whether it is in an array

php determines whether it is in an array

WBOY
WBOYOriginal
2023-05-19 13:31:08543browse

PHP, as a high-performance scripting language, has been widely used in processing arrays. In actual development, developers need to always understand the related operation methods of arrays, and judging whether an element is in an array is a very important operation. So, in PHP, how to determine whether an element is in an array?

1. Use the in_array function

The in_array function is a built-in function of PHP. Its function is to determine whether the specified element is in the array. The syntax format of the in_array function is as follows:

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

Description:

  • $needle: The value to be found (required)
  • $haystack: The array to be found ( Required)
  • $strict: Optional parameter. If this parameter is TRUE, the in_array function will check the type of needle (such as integer, string, and boolean) like the "===" operator; if this parameter is FALSE, the in_array function will only check the value of needle without Check its data type.

The return result of the in_array function is of BOOL type. When the specified element is in the array, TRUE is returned. Returns FALSE when the specified element is not in the array.

In actual operation, we can use the in_array function to determine whether an element is in an array. The code example is as follows:

$arr = array('apple', 'banana', 'grape', 'orange');

if (in_array('banana', $arr)) {
    echo '元素存在于数组中';
} else {
    echo '元素不存在于数组中';
}

In this example, we define an element that includes 4 elements. array, and then use the in_array function to find whether the "banana" element exists in this array. If it exists, output "the element exists in the array", otherwise output "the element does not exist in the array".

2. Use the array_search function

Similar to the in_array function, the array_search function is another built-in function in PHP. It can also be used to determine whether the specified element is in an array. The syntax format of the array_search function is as follows:

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

Description:

  • $needle: The value to be found (required)
  • $haystack: The array to be found ( Required)
  • $strict: Optional parameter. If this parameter is TRUE, the array_search function will check the type of needle (such as integer, string and boolean) like the "===" operator; if this parameter is FALSE, the array_search function will only check the value of needle without Check its data type.

The return result of this function is the key of the element in the array. If the specified element is not in the array, it returns FALSE.

In actual development, we can also use the array_search function to determine whether an element is in an array. The code example is as follows:

$arr = array('apple', 'banana', 'grape', 'orange');

if (array_search('banana', $arr) !== false) {
    echo '元素存在于数组中';
} else {
    echo '元素不存在于数组中';
}

In this example, we also define an element including 4 An array of elements, and then use the array_search function to find whether the "banana" element exists in this array. If it exists, output "Element exists in array", otherwise output "Element does not exist in array". It should be noted that in this example, we use the "!== false" operator to determine whether the return value is equal to false. This is because the return type of the array_search function is mixed, which may be the key of the element in the array, or it may be FALSE. Using "!== false" ensures that the return value is of type bool. If we use "if (array_search('banana', $arr))" to judge the return value, we may get wrong results.

3. Use isset function and key name

The third way to determine whether an element is in the array is to use isset function and key name. The principle of this method is that when an undefined variable is used for isset operation, the operation will return FALSE. In an array, if an element does not exist, the corresponding key is also undefined.

Therefore, we can use the isset function and key name to determine whether an element is in the array. The code example is as follows:

$arr = array('apple', 'banana', 'grape', 'orange');

if (isset($arr['banana'])) {
    echo '元素存在于数组中';
} else {
    echo '元素不存在于数组中';
}

In this example, we also define an element that contains 4 elements. array, and then use the isset function and key name to find whether the "banana" element exists in this array. If it exists, output "the element exists in the array", otherwise output "the element does not exist in the array".

It should be noted that if you use this method to determine whether an element does not exist in the array, the code example is as follows:

$arr = array('apple', 'banana', 'grape', 'orange');

if (isset($arr['melon'])) {
    echo '元素存在于数组中';
} else {
    echo '元素不存在于数组中';
}

If the element does not exist in the array, the isset function returns FALSE. But this does not actually mean that the element is not in the array. Therefore, this method is only suitable for determining whether an element is in the array, and is not suitable for determining whether an element does not exist in the array.

Summary

Determining whether an element is in an array is a very important operation in PHP. This operation can be achieved by using the in_array function, array_search function, isset function and key name. When making judgments, we need to choose the appropriate method according to the actual situation and pay attention to the conversion of the return value type. These operations can improve the stability and scalability of the code and help us better develop high-quality PHP applications.

The above is the detailed content of php determines whether it is 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:Fuzzy query in php arrayNext article:Fuzzy query in php array