Home  >  Article  >  Backend Development  >  How to determine whether a php array contains a string

How to determine whether a php array contains a string

PHPz
PHPzOriginal
2023-04-18 10:18:24573browse

PHP is a scripting language commonly used for web development. In web development, arrays are often used in PHP to store and manage data. Sometimes you need to determine whether an array contains a specific string. This article will introduce in detail how to determine whether an array contains a specified string in PHP, and provide example code and detailed explanations.

In PHP, we can use the in_array function to determine whether an array contains a specified string. The syntax of the in_array function is as follows:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Among them, $needle represents the value that needs to be searched, $haystack represents the array that needs to be searched, and $strict is an optional parameter. If set to TRUE, the type is also checked. The in_array function returns a Boolean value that returns true if the array contains the searched value; otherwise it returns false.

First, let's look at a simple example: determine whether an array contains the string "apple".

<?php
$fruits = array("apple", "banana", "cherry", "durian");
 
if (in_array("apple", $fruits)) {
    echo "包含字符串&#39;apple&#39;。";
} else {
    echo "不包含字符串&#39;apple&#39;。";
}
?>

The output result of the code is: containing the string 'apple'. Because we defined "apple" in the array $fruits, when using the in_array function, it returns true and the output contains the string 'apple'.

So, what if we need to find whether multiple strings are in an array? We can use a loop to judge one by one.

The following is an example code:

<?php
$fruits = array("apple", "banana", "cherry", "durian");

$find_fruits = array("apple", "pear");

foreach ($find_fruits as $fruit) {
    if (in_array($fruit, $fruits)) {
        echo "包含字符串&#39;$fruit&#39;。";
    } else {
        echo "不包含字符串&#39;$fruit&#39;。";
    }
}
?>

The output result is:

Contains the string 'apple'. Does not contain the string 'pear'.

In the above code, we define two arrays: $fruits contains some fruit names, and $find_fruits is the name of the fruit that needs to be found. Then we used a foreach loop to iterate through the names of the fruits we need to find, and then used the in_array function to determine whether they are included. If included, the output contains the string 'x', if not included, the output does not contain the string 'x'.

This example is very simple, but it can be easily seen how we use the in_array function to determine whether an array contains multiple strings.

Sometimes, we need to find a value in a custom array, such as the following code:

<?php
$users = array(
        array(&#39;name&#39; => '张三', 'age' => 20),
        array('name' => '李四', 'age' => 22),
        array('name' => '王五', 'age' => 25),
    );
 
$find_name = '李四';
 
foreach($users as $data){
    if(in_array($find_name, $data, true)){
        echo "该数组包含$find_name";
    }
}
?>

In the above code, we define a two-dimensional array called $users, each of which The subarrays each contain a name and age. Then $find_name is defined, which is the value we need to find, that is, the name we need to find. We use a foreach loop to iterate through the array $users and then use the in_array function to find $find_name. If $find_name is in the array $users, output "The array contains $find_name". Otherwise, no action is performed.

The in_array function is very useful when determining whether a string is in an array. It allows us to quickly determine whether an array contains a specified string. At the same time, we can also use a loop to find whether multiple strings are in an array. By using these techniques, we can manage and find data in arrays more efficiently in PHP.

The above is the detailed content of How to determine whether a php array contains a string. 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