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

How to determine whether a string exists in an array in php

PHPz
PHPzOriginal
2023-04-26 14:22:49767browse

In PHP, sometimes we need to determine whether a string exists in an array. This situation is very common in programming. For example, we need to determine whether the content entered by the user is a specific keyword, or we need to check whether a file name contains a specific file extension, etc. Whenever we need to find a specific string in a large array, we need to have a fast and reliable way to tell whether the string is present in the array.

PHP provides several methods to determine whether a string exists in an array. Here are some of them:

1. Using the in_array() function

The in_array() function is a function in PHP used to check whether a value exists in an array. Its syntax is as follows:

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

where $needle is the value to be found, $haystack is the value to be found in In the array to be searched, $strict is an optional parameter used to indicate whether to perform strict comparison. If the $strict argument is TRUE, the in_array() function will consider the type when looking, otherwise it will not.

The following is a simple example of using the in_array() function to determine whether a string exists in an array:

$keywords = array("PHP", "MySQL", "HTML" , "CSS");
$searchTerm = "MySQL";
if (in_array($searchTerm, $keywords)) {

echo "字符串$searchTerm存在于数组中";

} else {

echo "字符串$searchTerm不存在于数组中";

}

In the above example, we first define an array $keywords and add several elements to it. Then, we define the string we want to search for, $searchTerm. Next, we use the in_array() function to check if $searchTerm exists in the $keywords array. If $searchTerm exists in the $keywords array, the string "$searchTerm exists in the array" will be output, otherwise the string "$searchTerm does not exist in the array" will be output.

2. Use the array_search() function

The array_search() function is another function in PHP used to find the position of a specific value in an array. Its syntax is as follows:

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

where $needle is the value to be found, $haystack is the value to be found. In the array to be searched, $strict is an optional parameter used to indicate whether to perform strict comparison. If the $strict argument is TRUE, the in_array() function will consider the type when looking, otherwise it will not.

The following is a simple example of using the array_search() function to determine whether a string exists in an array:

$keywords = array("PHP", "MySQL", "HTML" , "CSS");
$searchTerm = "MySQL";
$searchResult = array_search($searchTerm, $keywords);
if ($searchResult !== false) {

echo "字符串$searchTerm存在于数组中,在位置$searchResult";

} else {

echo "字符串$searchTerm不存在于数组中";

}

In the above example, we first define an array $keywords and the string to be found $searchTerm. We then use the array_search() function to find the position of $searchTerm in the $keywords array. If $searchTerm exists in the $keywords array, then the $searchResult variable will contain its position and the string "$searchTerm exists in the array at position $searchResult" is output. Otherwise, the string "$searchTerm does not exist in the array" will be output.

3. Use the in_array() and array_keys() functions

Another way to find a specific string in an array is to use a combination of the in_array() and array_keys() functions. This method first uses the in_array() function to check if the string exists in the array, if it does then uses the array_keys() function to get all its positions.

The following is a simple example using the in_array() and array_keys() functions to determine whether a string exists in an array:

$keywords = array("PHP", "MySQL" , "HTML", "CSS", "PHP");
$searchTerm = "PHP";
if (in_array($searchTerm, $keywords)) {

$positions = array_keys($keywords, $searchTerm);
echo "字符串$searchTerm出现在数组中的位置:" . implode(",", $positions);

} else {

echo "字符串$searchTerm不存在于数组中";

}

In the above example, we have provided an array $keywords containing multiple elements. We then define the string we want to find $searchTerm and use the in_array() function to check if the string exists in the $keywords array. If $searchTerm exists in the $keywords array, then we use the array_keys() function to get the positions of all $searchTerms in the array and use the implode() function to output it as a comma-separated string. Otherwise, the string "$searchTerm does not exist in the array" will be output.

Summary

In PHP, there are several ways to determine whether a string exists in an array. Writing code using any of these methods should be fast and reliable. Using the in_array() function you can directly check if a string exists in an array. Using the array_search() function you can find the position of a string in an array. Finally, if you need to get the position of all specific strings in an array, you can combine the in_array() and array_keys() functions.

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