Home  >  Article  >  Backend Development  >  How to determine whether a string is contained in an array in php

How to determine whether a string is contained in an array in php

PHPz
PHPzOriginal
2023-04-23 09:12:38633browse

In php, we often need to determine whether a string is contained in an array. This problem may be difficult for beginners, but the solution to this problem is actually very simple. In this article, we will introduce several methods in PHP to determine whether a string is contained in an array.

  1. in_array function

The in_array function is a function in PHP that is used to determine whether a value exists in an array. The syntax of this function is:

in_array($needle, $haystack, $strict)

where $needle represents the value to be found, $haystack represents the array to be found, and $strict represents Whether to use strict mode for searching. If $strict is true, the in_array function also compares the data types of the variables for equality. A return value of true indicates that the value was found, false indicates that it was not found.

The following is a simple example:

$arr = array('apple', 'banana', 'orange');
if (in_array('banana', $arr)) {
    echo 'find it!';
} else {
    echo 'not found';
}

The running result is: find it!

  1. array_search function

array_search function too Function used to find a specified value in an array. The syntax of this function is:

array_search($needle, $haystack, $strict)

where $needle represents the value to be searched, $haystack represents the array to be searched, and $strict represents Whether to use strict mode for searching. Returns the position of the value in the array, or false if not found.

The following is a simple example:

$arr = array('apple', 'banana', 'orange');
$index = array_search('banana', $arr);
if ($index !== false) {
    echo 'find it at index ' . $index;
} else {
    echo 'not found';
}

The running result is: find it at index 1

It should be noted that the array_search function returns the value in the array location, not whether the value exists. Therefore, you need to use $index !== false to determine whether the value exists.

  1. Using regular expressions

Regular expression is a powerful string matching tool and is often used to process strings in PHP. If we want to determine whether a string is contained in an array, we can also use regular expressions to achieve this.

Specifically, we can separate all elements in the array with vertical bars (|), generate a regular expression, and then use the preg_match function to match the string.

The following is a simple example:

$arr = array('apple', 'banana', 'orange');
$regex = '/^(?:' . implode('|', $arr) . ')$/';
if (preg_match($regex, 'banana')) {
    echo 'find it!';
} else {
    echo 'not found';
}

In the above code, the implode function is used to separate all elements in the array with vertical bars to generate a regular expression. We then use the preg_match function to match the string and return true if found, otherwise false.

It should be noted that the regular expression we generated uses the delimiters (^ and $) at the beginning and end, which means that the regular expression can only match the entire string, not within the string. a part of. In this way, when finding whether a string is contained in an array, there will be no cases such as "grape" being misjudged as "raspberry".

Conclusion

Through the above three methods, we can easily determine whether a string is contained in an array. Directly using the in_array function and array_search function is the simplest method, while using regular expressions can meet more complex needs. At the same time, we also need to pay attention to the advantages and disadvantages of various methods and choose the method that suits us to solve the problem.

The above is the detailed content of How to determine whether a string is contained 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