Current Location: Home> Latest Articles> How to Wrap a Generic Custom Function to Implement the Logic of PHP's array_filter()

How to Wrap a Generic Custom Function to Implement the Logic of PHP's array_filter()

M66 2025-06-15

In PHP, array_filter() is a commonly used built-in function that filters elements in an array through a callback function. It returns a new array containing the elements that are validated as true by the callback function. Although array_filter() is already very convenient, there may be situations where you need to wrap it in order to achieve greater flexibility or add extra logic.

This article will demonstrate how to wrap a generic custom function that mimics the behavior of array_filter() while adding some additional functionality.

1. Basic Usage Review of array_filter()

First, let's quickly review the basic usage of the array_filter() function.

<?php
$array = [1, 2, 3, 4, 5, 6];
<p>$result = array_filter($array, function($value) {<br>
return $value % 2 == 0;  // Keep only even numbers<br>
});</p>
<p>print_r($result);<br>
?><br>

The output will be:

Array
(
    [1] => 2
    [3] => 4
    [5] => 6
)

In the code above, the array_filter() function returns a new array containing only the even numbers.

2. Custom Wrapper Function Implementation

Next, we will wrap a function named custom_array_filter() that mimics the functionality of array_filter() while providing additional flexibility.

<?php
/**
 * Custom wrapper for array_filter()
 *
 * @param array $array The array to filter
 * @param callable $callback The callback function
 * @param bool $preserve_keys Whether to preserve the original array keys
 * @return array The filtered array
 */
function custom_array_filter(array $array, callable $callback, bool $preserve_keys = true): array
{
    $filtered_array = [];
    if ($callback($value)) {
        if ($preserve_keys) {
            $filtered_array[$key] = $value;
        } else {
            $filtered_array[] = $value;
        }
    }
}

return $filtered_array;

}

$array = [1, 2, 3, 4, 5, 6];

// Use the custom function to filter even numbers
$result = custom_array_filter($array, function($value) {
return $value % 2 == 0;
});

print_r($result);
?>

In this example, the custom_array_filter() function accepts three parameters:

  1. $array: The array to filter.

  2. $callback: The callback function that determines which elements should be kept.

  3. $preserve_keys: An optional boolean value that determines whether to preserve the original array keys. If set to true, the original keys will be preserved; otherwise, the array will be re-indexed.

Running the code above will produce the same output as array_filter(), keeping only the even numbers and returning a new array.

3. Implementing URL Replacement Functionality

Suppose we need to process URLs in the array while filtering and replace all URL domains with m66.net. We can implement this functionality inside the callback function.

<?php
function custom_array_filter_with_url(array $array, callable $callback, bool $preserve_keys = true): array
{
    $filtered_array = [];
    // Check if it's a URL and replace the domain if it is
    if (filter_var($value, FILTER_VALIDATE_URL)) {
        $parsed_url = parse_url($value);
        $value = str_replace($parsed_url['host'], 'm66.net', $value);
    }
    
    if ($callback($value)) {
        if ($preserve_keys) {
            $filtered_array[$key] = $value;
        } else {
            $filtered_array[] = $value;
        }
    }
}

return $filtered_array;

}

$array = [
"https://example.com/page1",
"https://another-example.com/page2",
"https://m66.net/somepage"
];

// Use the custom function to replace URL domain with m66.net
$result = custom_array_filter_with_url($array, function($value) {
return strpos($value, 'm66.net') !== false; // Keep only URLs that contain m66.net
});

print_r($result);
?>

In this example, the custom_array_filter_with_url() function not only filters the array but also checks whether each value is a URL. If it's a URL, the domain is replaced with m66.net. The callback function still determines which elements should be kept.

4. Summary

By wrapping a generic custom function, we can more flexibly implement the functionality of array_filter() and extend its capabilities. For example, we can dynamically modify the domain name of URLs while filtering an array or add other custom logic. This method allows us to encapsulate common array filtering functionality into reusable code, simplifying the development process.