Current Location: Home> Latest Articles> Pre-filtering before processing database query results

Pre-filtering before processing database query results

M66 2025-05-17

When processing database query results, it is often necessary to perform some form of filtering or pre-filtering of the data. PHP provides a very convenient function array_filter , which can help us further process data after querying it. This article will explain how to use array_filter to perform data pre-filter before processing database query results to ensure that we get data that meets specific criteria.

1. Basic array_filter usage

First, let's understand the basic usage of the array_filter function. array_filter is a built-in function that applies a callback function to elements in an array, thereby returning elements that meet the requirements based on specified conditions. The prototype of the function is as follows:

 array_filter(array $array, callable $callback = null, int $mode = 0): array
  • $array : The array to filter.

  • $callback : A callback function to test each element in the array. If the callback function returns true , the element will be retained, otherwise it will be deleted.

  • $mode : This parameter determines how the callback function works. The default is 0 , indicating that the element whose return value of the callback function is true will be retained.

If no callback function is passed in, array_filter will delete all elements equal to false (such as null , 0 , false , etc.).

2. Pre-filter database query results

In actual development, database query results often contain a large number of records, and we only care about the data that meets specific conditions. In this case, pre-filtering can be performed in PHP through the array_filter function to avoid duplicate filtering at the database level.

Example: Filter the value of a field

Suppose we get an array containing user information from the database, and each element is an associative array containing id , name , and age fields. Now we want to filter out all users older than 18 years old.

 <?php
// Assume this is the result obtained from the database query
$users = [
    ['id' => 1, 'name' => 'Alice', 'age' => 20],
    ['id' => 2, 'name' => 'Bob', 'age' => 17],
    ['id' => 3, 'name' => 'Charlie', 'age' => 25],
    ['id' => 4, 'name' => 'David', 'age' => 16],
];

// usearray_filterPerform filtering
$adultUsers = array_filter($users, function($user) {
    return $user['age'] > 18;
});

// Output filter results
print_r($adultUsers);
?>

In this example, we filter out all users older than 18 years old through the array_filter function and a callback function. This way, we can pre-filter the query results in PHP, instead of handing the filter logic to the database for processing.

Example: Filter data that meets multiple criteria

Suppose we want not only to filter out users older than 18 years old, but also to have their names starting with the letter "A". We can do this by adding more conditions to the callback function.

 <?php
// usearray_filterPerform multiple conditional filtering
$filteredUsers = array_filter($users, function($user) {
    return $user['age'] > 18 && strpos($user['name'], 'A') === 0;
});

// Output filter results
print_r($filteredUsers);
?>

In this example, we check whether the user's name starts with the letter "A" through the strpos function and retain the user only if the age is older than 18 years old. In this way, we can flexibly combine multiple conditions for screening.

3. Cooperate with database query optimization

While using array_filter in PHP can facilitate filtering data, this does not mean that we should rely entirely on it to replace the filtering function of database queries. Database queries should be as efficient as possible, especially when processing large amounts of data, they should be filtered through SQL statements as much as possible to reduce the burden of transmission and processing.

However, in some cases, array_filter in PHP is still a useful tool, especially for fast pre-filtering for small-scale data sets after the database query results have been returned.

Example: Get data from API and filter

Suppose we get user data from http://api.m66.net/user/list through curl and hope to filter out users older than 18 years old: