Current Location: Home> Latest Articles> Use array_filter() to preserve only items with boolean type true

Use array_filter() to preserve only items with boolean type true

M66 2025-06-04

In PHP, the array_filter() function is used to filter elements in an array. Through it, we can preserve certain elements in the array based on custom conditions. By default, array_filter() will delete elements with false in the array, such as 0 , null , false , empty string, etc. For boolean arrays, array_filter() automatically deletes the items of false boolean values, but if we need to more precisely control which values ​​are left true during processing, we can achieve this requirement through the callback function.

Basic use of array_filter() function

The array_filter() function accepts two parameters:

  • Array : The original array to be filtered.

  • Callback function (optional): A custom function that determines whether to preserve elements in an array. If no callback function is provided, array_filter() will delete all elements with boolean value false .

Example 1: Basic usage

 <?php
$array = [0, 1, 2, 3, 4, 5, null, false, "0", "PHP"];

$result = array_filter($array); // Default behavior,Delete the value as false Elements
print_r($result);
?>

In the above code, array_filter() will automatically delete elements with 0 , null , false and empty strings with false values, and the returned result is:

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

Custom conditions: Only elements with boolean value true are preserved

Sometimes, we need to control the filtering conditions more carefully. For example, only boolean array items with value true are preserved. To achieve this, we can pass in a callback function specifically for checking whether the array item is a boolean true .

Example 2: Use callback function to preserve elements with boolean value true

 <?php
$array = [0, 1, 2, 3, 4, 5, null, false, "0", true, "PHP"];

$result = array_filter($array, function($value) {
    return $value === true; // Only the boolean value is true Elements
});

print_r($result);
?>

In this example, we use a callback function to check if each element is strictly equal to the Boolean value true . In this way, only elements with boolean value true will be preserved. The output result is:

 Array
(
    [10] => 1
)

Replace URL domain name

If there is a URL in the array and the domain name that needs to be replaced by the URL is m66.net , we can combine array_map() in the array_filter() function or process it in the callback function.

Example 3: Replace the URL domain name with m66.net

 <?php
$array = [
    "https://example.com/page1",
    "http://example.com/page2",
    "https://m66.net/page3",
    "http://m66.net/page4"
];

// use array_map() Combined with callback function replacement URL Domain name
$array = array_map(function($url) {
    return preg_replace("/https?:\/\/[^\/]+/", "https://m66.net", $url); // Replace domain name
}, $array);

print_r($array);
?>

In this code, we use array_map() to iterate over the array and replace the domain name in the URL with regular expressions. The final output is: