Home > Backend Development > PHP Problem > How to check if an array contains a string in php

How to check if an array contains a string in php

PHPz
Release: 2023-04-26 14:15:07
Original
957 people have browsed it

In PHP development, we often need to determine whether an array contains a specific string. This article will introduce you to several methods to achieve this function.

Method 1. Use the in_array() function

The in_array() function is used to determine whether an element exists in the array. If it exists, it returns True, otherwise it returns False. You can use this function to determine whether an array contains a certain string. The specific method is as follows:

<?php
$arr = array(&#39;apple&#39;, &#39;banana&#39;, &#39;peach&#39;, &#39;watermelon&#39;);
$fruit = &#39;banana&#39;;
if (in_array($fruit, $arr)) {
    echo &#39;数组中包含字符串&#39; . $fruit;
} else {
    echo &#39;数组中不包含字符串&#39; . $fruit;
}
?>
Copy after login

In the above code, we first define an array $arr, and then define a $fruit variable, which represents the The string to search for. After that, we use the in_array() function to determine whether $fruit exists in the array. If it exists, output ‘the array contains the string banana’, otherwise output ‘the array does not contain the string banana’.

Method 2. Use the array_search() function

The array_search() function is used to find a value in the array and return its corresponding key name. If it is not found, it returns False . You can use this function to determine whether an array contains a certain string. The specific method is as follows:

<?php
$arr = array(&#39;apple&#39;, &#39;banana&#39;, &#39;peach&#39;, &#39;watermelon&#39;);
$fruit = &#39;banana&#39;;
$key = array_search($fruit, $arr);
if ($key !== False) {
    echo &#39;数组中包含字符串&#39; . $fruit . &#39;,对应的键名为&#39; . $key;
} else {
    echo &#39;数组中不包含字符串&#39; . $fruit;
}
?>
Copy after login

In the above code, we also first define an array $arr and a variable $fruit to represent the character to be found. string. Next, we use the array_search() function to find the position of $fruit in the array. If found, it will output ‘the array contains the string banana, and the corresponding key name is 1’, otherwise it will output ‘the array does not contain the string banana’.

It should be noted that the array_search() function can use strict mode, that is, in addition to comparing values, it is also necessary to compare whether the data types are the same. The specific method is as follows:

<?php
$arr = array(0 => 'apple', '1' => 'banana', '2' => 'peach', '3' => 'watermelon');
$fruit = '1';
$key = array_search($fruit, $arr, True);
if ($key !== False) {
    echo '数组中包含字符串' . $fruit . ',对应的键名为' . $key;
} else {
    echo '数组中不包含字符串' . $fruit;
}
?>
Copy after login

In the above code, we define an array in line 3, in which the numeric key names are all string types. We defined the value of the variable $fruit as '1' of string type and set the strict mode option to True. After that, we use the array_search() function to find the position of $fruit in the array and output the result.

Method 3. Use regular expressions

Regular expression is a powerful text matching tool. We can use regular expressions to determine whether an array contains a specific string. The specific method is as follows:

<?php
$arr = array(&#39;apple&#39;, &#39;banana&#39;, &#39;peach&#39;, &#39;watermelon&#39;);
$fruit = &#39;/^banana$/&#39;;
foreach ($arr as $item) {
    if (preg_match($fruit, $item)) {
        echo &#39;数组中包含字符串&#39; . $item;
        break;
    }
}
?>
Copy after login

In the above code, we first define an array $arr and a $fruit variable. After that, we iterate through each element in the array and use the preg_match() function to determine whether the current element meets the matching rules of the regular expression $fruit. If found, output the element and exit the loop, otherwise it will prompt that no matching string was found.

It should be noted that although regular expressions are powerful, they may affect the performance of the program in some cases, so you need to be careful when using them.

To sum up, we have introduced three methods to determine whether an array contains a specific string. In actual development, we can choose the appropriate method according to our own needs.

The above is the detailed content of How to check if an array contains a string in php. 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