Home > Backend Development > PHP Problem > PHP determines whether a string exists in an array

PHP determines whether a string exists in an array

WBOY
Release: 2023-05-22 18:20:37
Original
818 people have browsed it

In PHP development, it is often necessary to find whether a specific string exists from a string array. This kind of operation is very common. After all, in many scenarios, it is necessary to filter or filter based on a certain keyword. So, how to determine whether a certain string exists in the array? This article will introduce you to some practical methods.

Method 1: Use the in_array function

PHP has built-in a very convenient function in_array, which is used to determine whether an element is in an array. Use this function to easily determine whether a string exists in an array. The following is the basic syntax for using the in_array function:

in_array($needle, $haystack); 
Copy after login

Among them, $needle is the string to be searched, and $haystack is the array to be searched. Returns true if $needle exists in the $haystack array, false otherwise.

For example, the following is an array composed of three strings. We need to determine whether the string "blue" exists in the array:

$colors = array("red", "green", "blue");
if(in_array("blue", $colors)){
    echo "字符串'blue'存在于数组中";
} else {
    echo "字符串'blue'不存在于数组中";
}
Copy after login

The output result is: string 'blue' exists in the array

In addition, in_array also supports the third parameter $strict, which is used to indicate whether to strictly match. By default, this parameter is false, which means that as long as $needle appears in $haystack, the match is considered successful. However, if the third parameter is set to true, it is not only required that $needle appears in $haystack, but also that the elements in $needle and $haystack are of the same type. For example:

$a = array(1, 2, '3');
if (in_array('3', $a, true)) {
    echo '"3" is in $a with strict check';
} else {
    echo '"3" is not in $a with strict check';
}
Copy after login

The output result is: "3" is not in $a with strict check

Method 2: Use array_search function

array_search function is used to search in arrays A value and returns the key name of that value. Unlike the in_array function, this function returns the matched key name instead of the true or false value. If the element is not found, returns false. The following is the basic syntax for using the array_search function:

array_search($needle, $haystack); 
Copy after login

where $needle is the string to be searched, and $haystack is the array to be searched. If $needle exists in the $haystack array, return the key name corresponding to the element, otherwise return false.

For example, the following is an array composed of three strings. We need to determine whether the string "blue" exists in the array:

$colors = array("red", "green", "blue");
$key = array_search("blue", $colors);
if ( $key !== false ) {
    echo "字符串'blue'存在于数组中,对应键名为".$key;
} else {
    echo "字符串'blue'不存在于数组中";
}
Copy after login

The output result is: string 'blue' exists in the array, and the corresponding key name is 2

Method 3: Use in_array and array_map combined

If you want to determine whether a string exists in multiple arrays, instead of just To search in an array, you can use the combination of in_array and array_map functions. The following is the basic syntax for using this method:

function check_string_one_of_many_arrays($string, $arrays) {
    return count(array_filter($arrays, function($array) use ($string) {
        return in_array($string, $array);
    })) > 0;
}
Copy after login

Among them, $string is the string that needs to be searched, and $arrays is the array collection that needs to be searched. This function returns a Boolean value indicating whether the string exists in multiple arrays.

For example, there are now two arrays $colors1 and $colors2, and it is necessary to determine whether the string "blue" exists in them:

$colors1 = array("red", "green", "blue");
$colors2 = array("yellow", "black", "white");
if(check_string_one_of_many_arrays("blue", array($colors1, $colors2))){
    echo "字符串'blue'存在于数组中";
} else {
    echo "字符串'blue'不存在于数组中";
}
Copy after login

The output result is: the string 'blue' exists In an array

Summary

The above are three common ways to determine whether a string exists in an array. Depending on the needs of different scenarios, you can choose to use different methods. No matter which method is used, judging whether a string exists in an array is very simple and easy to understand, and it is worthy of widespread application in our daily PHP programming.

The above is the detailed content of PHP determines whether a string exists in an array. 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