How to search multiple values ​​in PHP array

WBOY
Release: 2023-07-07 16:26:01
Original
1520 people have browsed it

How to search for multiple values ​​in a PHP array

When developing web applications, it is often necessary to search for multiple values ​​in an array. PHP provides some built-in functions and techniques that can help us implement this function quickly and efficiently. This article will introduce several commonly used methods and corresponding code examples.

Method 1: Use array_intersect function

The array_intersect function is used to calculate the intersection of arrays. It accepts multiple arrays as parameters and returns a new array containing values ​​that exist in all arrays. We can form an array with the values ​​to be searched, and then perform an intersection operation with the target array to finally get the value that meets the conditions.

The following is a sample code:

$values = array(1, 2, 3, 4, 5);
$search = array(2, 4);

$result = array_intersect($values, $search);

print_r($result);
Copy after login

In the above code, $values ​​is the target array, and $search is an array of values ​​to be searched. The result will be a new array containing 2 and 4.

Method 2: Use the array_filter function

The array_filter function can filter out the elements in the array that meet the conditions according to the specified conditions. We can use this function to search for multiple values.

The following is a sample code:

$values = array(1, 2, 3, 4, 5);
$search = array(2, 4);

$result = array_filter($values, function ($value) use ($search) {
    return in_array($value, $search);
});

print_r($result);
Copy after login

In the above code, we use an anonymous function and the use keyword to pass $search to the inside of the anonymous function so that it can access the $search array . The result will be a new array containing 2 and 4.

Method 3: Use foreach loop

We can also use foreach loop to traverse the target array and compare the search values ​​one by one. When the search value is in the target array, add it to the results array.

The following is a sample code:

$values = array(1, 2, 3, 4, 5);
$search = array(2, 4);

$result = array();

foreach ($values as $value) {
    if (in_array($value, $search)) {
        $result[] = $value;
    }
}

print_r($result);
Copy after login

In the above code, we create an empty array $result and use a foreach loop to traverse the $values ​​array. The search value is compared through the in_array function. If the search value is found in the target array, it is added to the $result array. The result will be a new array containing 2 and 4.

To summarize, we have introduced three methods of searching for multiple values ​​in a PHP array. Choosing the appropriate method according to the specific situation can improve the efficiency and readability of the code. I hope this article is helpful to the problems you encounter during development.

The above is the detailed content of How to search multiple values ​​in PHP array. 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