Home > Article > Backend Development > How to detect whether an array contains a string in php
Two detection methods: 1. Use the in_array function to detect whether the array contains a value of the specified type. The syntax is "in_array('string', array, true)". If the return value is TRUE, then Included, otherwise not included. 2. Use the array_search() function to detect whether the array contains a value of the specified type. The syntax is "array_search('string', array, true)". If the corresponding key name is returned, it is included. If FALSE is returned, it is not included.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php detects whether the array contains a certain There are two methods for strings:
Use the in_array function to detect
Use the array_search() function to detect
Method 1: Use the in_array function to detect
The in_array() function can find whether the array contains a certain value. If it exists, it returns TRUE, if it does not exist, it returns FALSE. .
in_array($needle, $array[, $strict = FALSE])
The parameter description is as follows:
$needle: is the value to be searched. If $needle is a string, the comparison is case-sensitive;
$array: is the array to be searched;
$strict: is an optional parameter, the default is FALSE.
If $strict The value is TRUE, the in_array() function will not only check the values in $needle and $array, but also compare their types for equality.
If you want to use the in_array function to detect whether the array contains a string, you only need to set the first parameter to the specified string and the third parameter to Is TRUE (used to ensure that the type of the element value and the search value are consistent)
<?php header("Content-type:text/html;charset=utf-8"); $array = array( 'name' =>'PHP中文网', 'url' =>'https://www.php.cn/', 'title' =>'PHP教程', 'pid' =>1234 ); var_dump($array); if(in_array('PHP教程', $array, true)){ echo '数组中包含字符串“PHP教程”<br><br>'; }else{ echo '数组中不包含字符串“PHP教程”<br><br>'; } if(in_array('1234', $array, true)){ echo '数组中包含字符串“1234”<br><br>'; }else{ echo '数组中不包含字符串“1234”<br><br>'; } ?>
Method 2: Use the array_search() function to detect
array_search() function can search for a given value in the array. If successful, it will return the first corresponding key name. The syntax format of this function is as follows:
array_search($needle, $haystack[, $strict = false])
The parameter description is as follows:
array_search() function returns the key of $needle if it is found, otherwise it returns False.
<?php header("Content-type:text/html;charset=utf-8"); $array = array( 'name' =>'PHP中文网', 'url' =>'https://www.php.cn/', 'title' =>'PHP教程', 'pid' =>1234, 'level' => 1111, ); var_dump($array); var_dump(array_search('1234', $array, true)); var_dump(array_search('PHP教程', $array)); var_dump(array_search('1111', $array, true)); var_dump(array_search(1111, $array, true)); ?>
Note: If $needle appears more than once in $haystack, only the first matching key will be returned. To return all keys that match a value, the array_keys() function should be used instead.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to detect whether an array contains a string in php. For more information, please follow other related articles on the PHP Chinese website!