Current Location: Home> Latest Articles> How to use array_filter() to implement simple search filtering

How to use array_filter() to implement simple search filtering

M66 2025-06-03

In PHP, array_filter() is a very practical function that is often used to filter data from an array based on certain conditions. This article will use a simple example to demonstrate how to use array_filter() to implement a basic search and filtering function.

What is array_filter() ?

array_filter() is a built-in function for filtering elements in an array. Its syntax is as follows:

 array_filter(array $array, ?callable $callback = null, int $mode = 0): array

in:

  • $array : the array to be filtered;

  • $callback : A callback function used to determine whether each element is retained;

  • $mode : Optional parameter to specify whether the callback function receives a key value, a key name, or both.

If the callback function is not passed, array_filter() will remove elements with a value of false (including null , false , empty string, 0, etc.).

Sample Scenario: Search Product List

Suppose we have a product list, each product is an array item containing name and category . We want to search for products with the word in the product name based on the keyword.

 $products = [
    ['name' => 'iPhone 15', 'category' => 'electronics'],
    ['name' => 'MacBook Pro', 'category' => 'electronics'],
    ['name' => 'Espresso Machine', 'category' => 'home appliances'],
    ['name' => 'Running Shoes', 'category' => 'fashion'],
    ['name' => 'iPhone Case', 'category' => 'accessories'],
];

Users pass in search keywords through URL:

 // Example URL:https://m66.net/search.php?keyword=iphone
$keyword = isset($_GET['keyword']) ? strtolower(trim($_GET['keyword'])) : '';

Next, use array_filter() to search:

 $filteredProducts = array_filter($products, function ($product) use ($keyword) {
    return strpos(strtolower($product['name']), $keyword) !== false;
});

Finally, we can output the filtered product list:

 foreach ($filteredProducts as $product) {
    echo "<p>" . htmlspecialchars($product['name']) . " - " . htmlspecialchars($product['category']) . "</p>";
}

Complete sample code

 <?php
// products.php
$products = [
    ['name' => 'iPhone 15', 'category' => 'electronics'],
    ['name' => 'MacBook Pro', 'category' => 'electronics'],
    ['name' => 'Espresso Machine', 'category' => 'home appliances'],
    ['name' => 'Running Shoes', 'category' => 'fashion'],
    ['name' => 'iPhone Case', 'category' => 'accessories'],
];

$keyword = isset($_GET['keyword']) ? strtolower(trim($_GET['keyword'])) : '';

$filteredProducts = array_filter($products, function ($product) use ($keyword) {
    return strpos(strtolower($product['name']), $keyword) !== false;
});
?>
<!DOCTYPE html>
<html>
<head>
    <title>Search results - m66.net</title>
</head>
<body>
    <h1>Search results</h1>
    <?php if (empty($keyword)): ?>
        <p>Please enter search keywords。</p>
    <?php elseif (empty($filteredProducts)): ?>
        <p>Not found with“<?php echo htmlspecialchars($keyword); ?>”Related products。</p>
    <?php else: ?>
        <?php foreach ($filteredProducts as $product): ?>
            <p><?php echo htmlspecialchars($product['name']); ?> - <?php echo htmlspecialchars($product['category']); ?></p>
        <?php endforeach; ?>
    <?php endif; ?>
</body>
</html>

Summarize

array_filter() is a very powerful tool function in PHP, especially suitable for handling array search and conditional filtering tasks. By combining anonymous functions and use syntax, we can quickly build flexible and concise filtering logic. The above search example is a practical small feature that is suitable for a simple product or article search system.

If you are developing a lightweight search module, try array_filter() , it may be better than you think!