Current Location: Home> Latest Articles> Use array_filter and array_fill_keys to create a cleaned default array

Use array_filter and array_fill_keys to create a cleaned default array

M66 2025-06-06

In PHP, array operations are very common tasks. Especially when we process user input or external data, we usually need to clean and filter the array. This article will explain how to use the array_filter and array_fill_keys functions in PHP to create a cleaned default array.

1. Introduction to array_filter and array_fill_keys

  • array_filter function: This function can filter elements in an array and determine which elements need to be retained or deleted based on the given callback function. If the callback function returns true , the element will be retained; if false , the element will be removed.

  • array_fill_keys function: This function is used to create an array, specify the key name and assign a default value to each key name. Its purpose is to quickly generate an array with default values, especially suitable for handling certain scenarios where predefined key names are required.

By combining these two functions, we can provide default values ​​for each possible key while cleaning the input data, ensuring that the final array is complete and canonical.

2. Sample Scenario

Suppose we have a user-submitted form data that contains multiple key-value pairs (such as name , email , and age ). We want to clean this data, remove invalid or null data, and provide default values ​​for missing keys.

We will use array_filter to filter out null or invalid data, and array_fill_keys to fill in the default values ​​for missing keys.

3. Code implementation

Here is a sample code that demonstrates how to implement this process using array_filter and array_fill_keys :

 <?php

// Assume this is the form data submitted by the user
$userInput = [
    'name' => 'John Doe',
    'email' => 'johndoe@example.com',
    'age' => '',  // Null value
    'address' => null,  // Null value
];

// Define the default key we need
$defaultKeys = ['name', 'email', 'age', 'address'];

// 1. use array_filter Clean the data
$filteredData = array_filter($userInput, function($value) {
    // 过滤掉Null value、null Or empty string
    return !empty($value);
});

// 2. use array_fill_keys Fill in default values ​​for missing keys
$cleanedData = array_fill_keys($defaultKeys, 'default_value');

// 3. Merge the filtered data into the default array
$finalData = array_merge($cleanedData, $filteredData);

// Print the final result
print_r($finalData);

?>

4. Code explanation

  1. Input data: $userInput is a simulated user-submitted array, including fields such as name , email , age , and address . Here we deliberately leave blank or set to null for the age and address fields, simulating the situation where the user has not filled in these data.

  2. Clean data: We filter out empty or invalid data through the array_filter function. In the callback function, !empty($value) ensures that only non-null values ​​are retained.

  3. Fill the default value: We use the array_fill_keys function to set a default value for all possible keys ( name , email , age and address ) (using 'default_value' here). This way, any uncommitted fields will be automatically filled with the default value.

  4. Merge array: Use array_merge to combine the cleaned data with the default value. When merged, valid data in $filteredData will override the corresponding items in the default array.

5. Output result

The final $finalData array will contain all valid user input data and fill the missing fields with default values. For example:

 Array
(
    [name] => John Doe
    [email] => johndoe@example.com
    [age] => default_value
    [address] => default_value
)

6. Summary

Through the combination of array_filter and array_fill_keys , we are able to efficiently process array data, filter invalid data, and provide reasonable default values ​​for missing fields. This approach is very useful when processing user input and external data, ensuring that our programs can still run stably when facing incomplete or irregular data.

<div style="border-top: 1px solid #ccc; margin: 20px 0;"></div>

Related links: