Home  >  Article  >  Backend Development  >  How to determine whether a certain value exists in an array in php

How to determine whether a certain value exists in an array in php

PHPz
PHPzOriginal
2023-04-26 09:10:46660browse

As a programming language widely used in web development, PHP provides a wealth of functions and methods to handle various data types, including arrays. In the use of arrays, it is a common requirement to determine whether a certain value exists in the array. So, how to implement this function in PHP? This article will introduce you to several commonly used methods.

in_array() function

PHP provides an in_array() function, which can easily determine whether a value exists in an array. The prototype of this function is as follows:

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

Among them, $needle represents the value to be searched, $haystack represents the array to be searched, and $strict represents whether to use strict mode. The default is false. If the value is found, return true; otherwise, return false.

For example, we have an array $fruits with the content ["apple", "banana", "pineapple", "orange"]. Now to find whether the value "banana" exists in it, you can use in_array () function, the code is as follows:

$fruits = array("apple", "banana", "pineapple", "orange");
if (in_array("banana", $fruits)) {
    echo "该值已存在于数组中";
} else {
    echo "该值不存在于数组中";
}

As can be seen from the above code, if the value "banana" exists, it will output "The value already exists in the array"; otherwise, it will output "The value does not exist" in the array".

It should be noted that the in_array() function uses non-strict mode by default, that is to say, if the value searched is inconsistent with the data type of an element in the array, it will also be considered to exist. If you want to use strict mode, you need to set the $strict parameter to true. For example:

$numbers = array(1, 2, 3, "4");
if (in_array("4", $numbers, true)) {
    echo "该值已存在于数组中";
} else {
    echo "该值不存在于数组中";
}

As can be seen from the above code, although there is an element in the array with a value of 4, because it is a string type and the value to be found is a numeric type, it is not in strict mode. The following will be considered as non-existent. If the $strict parameter is set to true, strict mode will be used to search, and "the value does not exist in the array" will be output.

array_search() function

Similar to the in_array() function, PHP also provides another function array_search(), which can find a value in an array and return the subscript of the value. . The prototype of this function is as follows:

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

Among them, $needle represents the value to be searched, $haystack represents the array to be searched, and $strict represents whether to use strict mode. The default is false. If the value is found, the subscript at which the value is located is returned; otherwise, false is returned.

For example, we have an array $numbers with the content [1, 2, 3, 4, 5]. Now we want to find whether there is an element with a value of 4 and output the subscript of the element. You can Using the array_search() function, the code is as follows:

$numbers = array(1, 2, 3, 4, 5);
if (($key = array_search(4, $numbers)) !== false) {
    echo "该值存在于数组中,其下标为" . $key;
} else {
    echo "该值不存在于数组中";
}

As can be seen from the above code, if an element with a value of 4 is found, "This value exists in the array, and its subscript is 3"; otherwise , output "The value does not exist in the array".

It should also be noted that the array_search() function will also use non-strict mode to search. If you want to use strict mode, you need to set the $strict parameter to true. For example:

$numbers = array(1, 2, 3, "4", 5);
if (($key = array_search("4", $numbers, true)) !== false) {
    echo "该值存在于数组中,其下标为" . $key;
} else {
    echo "该值不存在于数组中";
}

As you can see from the above code, in non-strict mode, you can find the element with the value "4" and output "This value exists in the array, and its subscript is 3". In strict mode, since the value to be found is of string type and the value of the element in the array is of numeric type and cannot be matched, "the value does not exist in the array" will be output.

isset() and array_key_exists() functions

In addition to the above two functions, you can also use the isset() and array_key_exists() functions to determine whether a key or subscript exists in the array .

isset() function can detect whether a variable has been set and is not null. If the variable has been set and is not null, returns true; otherwise, returns false. When checking whether a key exists in an array, you can use the isset() function. For example:

$person = array("name" => "Tom", "age" => 20);
if (isset($person["name"])) {
    echo "该键存在于数组中";
} else {
    echo "该键不存在于数组中";
}

As can be seen from the above code, if there is an element with the key "name", it will output "the key exists in the array"; otherwise, it will output "the key does not exist in the array" .

It should be noted that when the isset() function detects non-existent array elements, it will return false without throwing a warning. For example:

$numbers = array(1, 2, 3, 4, 5);
if (isset($numbers[5])) {
    echo "该下标存在于数组中";
} else {
    echo "该下标不存在于数组中";
}

As can be seen from the above code, since the element with subscript 5 does not exist in the array, "The subscript does not exist in the array" will be output.

Similar to the isset() function, the array_key_exists() function can also detect whether a key exists in an array. The prototype of this function is as follows:

bool array_key_exists ( mixed $key , array $array )

Among them, $key represents the key to be searched, and $array represents the array to be searched. If the key is found, returns true; otherwise, returns false.

For example, we have an array $person with the content ["name" => "Tom", "age" => 20]. Now we want to find whether there is an element with the key "name" in it. , and output whether it exists, you can use the array_key_exists() function, the code is as follows:

$person = array("name" => "Tom", "age" => 20);
if (array_key_exists("name", $person)) {
    echo "该键存在于数组中";
} else {
    echo "该键不存在于数组中";
}

As can be seen from the above code, if there is an element with the key "name", it will output "The key exists in the array" "; Otherwise, output "The key does not exist in the array".

Conclusion

In short, judging whether a certain value or key exists in an array is a common operation in PHP, and it is also easy to implement. In addition to the methods introduced above, there are many other functions and methods that can achieve this function. When using it, you can choose the most suitable method according to the actual situation to improve the efficiency and readability of the program.

The above is the detailed content of How to determine whether a certain value exists in an array in php. 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