How to query in php whether an array contains a specified string

PHPz
Release: 2023-04-12 11:17:33
Original
1100 people have browsed it

For PHP developers, operating arrays has become a part of daily work. Sometimes you may encounter a problem, that is, you need to query whether an array contains a certain string. Don't worry, PHP provides multiple solutions to deal with this problem.

Method 1: Use in_array()

The in_array() function in PHP can be used to query whether a value exists in an array. You can use this function to query whether an array contains a certain string. The specific usage is as follows:

$array = array('apple', 'banana', 'orange');
if (in_array('apple', $array)) {
    echo '数组中包含apple';
}
Copy after login

Note that the second parameter of the in_array() function is the array to be queried. Returns true if found, false otherwise.

Method 2: Use array_search()

There is also a function in the array that can be used to query whether a certain value exists in the array, that is the array_search() function. The specific usage is as follows:

$array = array('apple', 'banana', 'orange');
$index = array_search('apple', $array);
if ($index !== false) {
    echo '数组中包含apple,位置是' . $index;
}
Copy after login

array_search() function returns the serial number of the value to be found, and returns false if it cannot be found.

Method 3: Use strstr()

There is also a function in PHP that can be used to determine whether a string contains a certain substring, that is the strstr() function. Combined with the key-value pairs of the array, we can use this function to query whether an array contains a certain string. The specific usage method is as follows:

$array = array('Name' => '张三', 'Age' => '25', 'Gender' => '男');
if (strstr(join(',', $array), '张三')) {
    echo '数组中包含张三';
}
Copy after login

Note that this method uses the join() function to connect all the values ​​​​of the array to generate a new string, and then uses the strstr() function to query whether the new string contains the content you want to find. String.

The above are three methods for querying whether an array contains a certain string. You can choose one of them according to actual needs. Using these methods, you can easily query whether an array contains a certain string, improving the readability and maintainability of your code.

The above is the detailed content of How to query in php whether an array contains a specified string. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!