Current Location: Home> Latest Articles> Use array_diff_key() to compare configuration files with default configuration

Use array_diff_key() to compare configuration files with default configuration

M66 2025-05-15

During the development process, we often need to compare a configuration file with the default configuration to quickly find out the differences. For example, suppose you have a default configuration array and a user-modified configuration file array, you need to quickly find out which configuration items are modified, added, or deleted.

PHP provides many useful functions to handle this kind of task, and array_diff_key() is a very suitable tool.

What is array_diff_key() ?

array_diff_key() is a built-in function in PHP that compares the key names of two arrays and returns an array containing key-value pairs that exist in the first array but do not exist in the second array. Simply put, it can help us find out the parts with different key names in two arrays.

Example: How to compare configuration files with default configuration using array_diff_key()

Let's assume that there is a default configuration array $defaultConfig and a user-modified configuration array $userConfig . We want to quickly find out the different parts of the user configuration than the default configuration. It can be achieved by:

 <?php

// Default configuration
$defaultConfig = [
    'site_name' => 'My Website',
    'site_url' => 'https://www.example.com',
    'site_admin' => 'admin@example.com',
    'theme' => 'default',
    'timezone' => 'UTC',
];

// User Configuration
$userConfig = [
    'site_name' => 'Custom Website',
    'site_url' => 'https://m66.net', // Modified URL
    'theme' => 'dark', // Modified主题
    'timezone' => 'Asia/Shanghai',
];

// use array_diff_key() 比对配置文件和Default configuration
$differences = array_diff_key($userConfig, $defaultConfig);

// Output difference
echo "User Configuration与Default configuration的差异:\n";
print_r($differences);

?>

Code explanation

  1. Default configuration and user configuration : We define two arrays, representing the default configuration and the user modified configuration. The user configuration is basically the same as the default configuration's key names, but there are some differences (such as site_url and theme ).

  2. array_diff_key() function : We use array_diff_key() to compare $userConfig and $defaultConfig , which returns a new array containing key-value pairs in $userConfig but not in $defaultConfig .

  3. Output Difference : The difference section is output via print_r() so that we can clearly see the difference between the user configuration and the default configuration.

Output result

After executing the above code, the output will be:

 User Configuration与Default configuration的差异:
Array
(
    [site_name] => Custom Website
    [site_url] => https://m66.net
    [theme] => dark
)

From the results we can see that the parts in the user configuration that are different from the default configuration include:

  • site_name has been modified to "Custom Website"

  • site_url has been modified to " https://m66.net"

  • The theme has been modified to "dark"

Summarize

By using array_diff_key() we can quickly find the difference between two configuration arrays. This method is very suitable for comparing configuration files with default configurations, so that developers can understand the user's modifications and quickly adjust or optimize.