Home  >  Article  >  Backend Development  >  How to perform fuzzy query on array in php

How to perform fuzzy query on array in php

PHPz
PHPzOriginal
2023-04-26 10:28:151234browse

In PHP development, arrays are an inevitable part of us. When we need to query data in an array, we usually use fuzzy queries. This article will introduce how to perform fuzzy query of arrays in PHP.

1. What is array fuzzy query

Array fuzzy query, that is, data query based on keywords, does not require a complete match, only matching items are required to return results. In actual development, array fuzzy queries are often used to find data with similar characteristics.

For example, we have a contact group that contains multiple contact information, including name, phone number, email address and other information. We can use fuzzy queries to find a person's contact information, or even enter only part of a name or phone number to find results.

2. Use array_filter() for fuzzy query

PHP’s array_filter() function can conditionally filter each element in the array and return results that meet the conditions. We can use this function to implement fuzzy query of arrays.

Suppose we have a users array that contains multiple contact information:

$users = array(
    array('name' => '张三', 'phone' => '13811112222', 'email' => 'zhangsan@example.com'),
    array('name' => '李四', 'phone' => '13911113333', 'email' => 'lisi@example.com'),
    array('name' => '王五', 'phone' => '15011112222', 'email' => 'wangwu@example.com'),
    array('name' => '赵六', 'phone' => '18011114444', 'email' => 'zhaoliu@example.com')
);

Now, we want to implement the function of finding users based on keywords. We can define a function, pass in keywords and user arrays, and use the array_filter() function to filter elements in the array that meet the conditions.

function search_users($keyword, $users) {
    $result = array_filter($users, function($user) use ($keyword) {
        foreach($user as $value) {
            if(stripos($value, $keyword) !== false) {
                return true;
            }
        }
        return false;
    });
    return $result;
}

The above function will perform a loop comparison of each user information and return true when a match is found. The stripos() function can retrieve the first occurrence of a string in another string, case-insensitively.

We can call this function to perform fuzzy query:

$search_result = search_users('1111', $users);

This query will return all contact information whose phone number contains "1111".

Using the array_filter() function to implement fuzzy queries on arrays is simple and easy to use, but it also has a problem: when the amount of data in the array is large, performance may be affected. At this time, we can use another way to implement fuzzy query of the array.

3. Use regular expressions for fuzzy query

Regular expression is a tool used to match and replace text. In PHP, we can use the preg_grep() function to filter out elements that meet conditions in an array based on regular expressions.

Suppose we have a keywords array that contains multiple keywords:

$keywords = array('1111', 'san', 'y@example');

Now, we want to find all contact information that meets the keyword conditions in the users array. We can use regular expressions to achieve this function:

function search_users_regex($keywords, $users) {
    $result = array();
    foreach ($keywords as $keyword) {
        $pattern = '/' . preg_quote($keyword, '/') . '/i'; // i表示不区分大小写
        // preg_grep()返回满足条件的元素的数组
        $matches = preg_grep($pattern, $users);
        $result = array_merge($result, $matches);
    }
    return $result;
}

The above function will loop through the keywords array, generate a regular expression for each keyword, use preg_grep() to match, and contact the qualified Person information is merged into a result array.

We can call this function to perform fuzzy query:

$search_result = search_users_regex($keywords, $users);

This query will return all contact information that meets any keyword condition, including phone numbers containing "1111" and names containing "san", the email address contains the contact information of "y@example".

Using regular expressions to perform fuzzy queries on arrays can more accurately find results that meet the conditions, but it also has a disadvantage: the construction of regular expressions requires certain knowledge and skills. If it is not written well, the code will May become difficult to understand and maintain.

4. Summary

Array fuzzy query is one of the commonly used functions in PHP development, which can help us quickly find elements in the array that meet conditions. This article introduces two ways to implement array fuzzy query, namely using the array_filter() function and using regular expressions. In actual development, we can choose a suitable method for implementation based on actual needs. At the same time, we should also pay attention to the impact that fuzzy queries may have on performance and optimize query efficiency as much as possible.

The above is the detailed content of How to perform fuzzy query on array 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