How to detect whether an array contains a string in php

青灯夜游
Release: 2023-03-16 15:56:02
Original
6864 people have browsed it

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])
Copy after login

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中文网', 'url' =>'//m.sbmmt.com/', 'title' =>'PHP教程', 'pid' =>1234 ); var_dump($array); if(in_array('PHP教程', $array, true)){ echo '数组中包含字符串“PHP教程”

'; }else{ echo '数组中不包含字符串“PHP教程”

'; } if(in_array('1234', $array, true)){ echo '数组中包含字符串“1234”

'; }else{ echo '数组中不包含字符串“1234”

'; } ?>
Copy after login

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])
Copy after login

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中文网', 'url' =>'//m.sbmmt.com/', '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)); ?>
Copy after login

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!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!