Home >Backend Development >PHP Problem >How to detect whether an array contains a string in php

How to detect whether an array contains a string in php

青灯夜游
青灯夜游Original
2022-07-22 19:24:286980browse

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.

How to detect whether an array contains a string in php

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 is empty or FALSE, the in_array() function will only check whether the value of $needle is equal to the value in $array;
    • 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(
    &#39;name&#39;  =>&#39;PHP中文网&#39;,
    &#39;url&#39;   =>&#39;https://www.php.cn/&#39;,
    &#39;title&#39; =>&#39;PHP教程&#39;,
    &#39;pid&#39;   =>1234
);
var_dump($array);
if(in_array(&#39;PHP教程&#39;, $array, true)){
    echo &#39;数组中包含字符串“PHP教程”<br><br>&#39;;
}else{
    echo &#39;数组中不包含字符串“PHP教程”<br><br>&#39;;
}
if(in_array(&#39;1234&#39;, $array, true)){
    echo &#39;数组中包含字符串“1234”<br><br>&#39;;
}else{
    echo &#39;数组中不包含字符串“1234”<br><br>&#39;;
}
?>

How to detect whether an array contains a string in php

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:

  • $needle: is the value to be searched. If $needle is a string type, the search is case-sensitive;
  • $haystack: is an array;
  • $strict: Optional parameter, can be empty, default is False.
    • If $strict is False, array_search() will only compare values, not types, when searching for $needle in $haystack.
    • If $strict is True, array_search() compares both the value and type when searching for $needle in $haystack.

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(
    &#39;name&#39;  =>&#39;PHP中文网&#39;,
    &#39;url&#39;   =>&#39;https://www.php.cn/&#39;,
    &#39;title&#39; =>&#39;PHP教程&#39;,
    &#39;pid&#39;   =>1234,
     &#39;level&#39; => 1111,
);
var_dump($array);
var_dump(array_search(&#39;1234&#39;, $array, true));
var_dump(array_search(&#39;PHP教程&#39;, $array));
var_dump(array_search(&#39;1111&#39;, $array, true));
var_dump(array_search(1111, $array, true));
?>

How to detect whether an array contains a string in php

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!

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