Current Location: Home> Latest Articles> Implement key whitelist logic based on array_diff_ukey()

Implement key whitelist logic based on array_diff_ukey()

M66 2025-05-14

In daily development, we often encounter the need to keep only the specified "key" from an associative array, which is often called "key whitelist" logic. Although we can use array_intersect_key() to achieve this goal, today we will introduce a slightly "unpopular" but very flexible way: use array_diff_ukey() to implement key whitelists.

What is array_diff_ukey() ?

array_diff_ukey() is a function provided by PHP to compare the key names of two or more arrays and use a callback function to determine whether the key names are equal. This function returns a key-value pair that exists in the first array but does not exist in other arrays .

The syntax is as follows:

 array_diff_ukey(array $array1, array $array2, callable $key_compare_func): array

Implementation ideas

Although array_diff_ukey() is used to "exclude" the key, we can use it in turn to "keep" the whitelist key with a little "short". The principle is as follows:

  1. Build an array containing whitelist keys (can be an array of null values);

  2. Reversing logic through custom key comparison function, so that only keys in whitelisted arrays are retained;

  3. Use array_diff_ukey() to "remove" keys that are not in the whitelist from the original array.

Sample code

Here is a complete example, assuming we have a data array $data that submits the form, and we just want to preserve the keys 'username' and 'email' :

 <?php

// Simulated user submission data
$data = [
    'username' => 'john_doe',
    'email' => 'john@example.com',
    'password' => '123456',
    'redirect' => 'https://m66.net/welcome'
];

// Define whitelist key
$whitelistKeys = [
    'username' => true,
    'email' => true,
];

// array_diff_ukey Reverse processing logic
$filteredData = array_diff_ukey($data, $whitelistKeys, function ($key1, $key2) {
    // Keys retained in the whitelist
    return $key1 === $key2 ? 0 : -1;
});

// Remove those from the original array“Not on the whitelist”Keys
$cleanData = array_diff_key($data, $filteredData);

// Output result
print_r($cleanData);

/*
Output result:
Array
(
    [username] => john_doe
    [email] => john@example.com
)
*/
?>

Advantage analysis

The logic of implementing key whitelisting using array_diff_ukey() , although slightly tortuous, provides:

  • Stronger flexibility : can implement complex key matching logic, such as ignoring case, processing prefixes, etc.;

  • High readability (when used with comments);

  • No side effects : The original array will not be modified.

Application scenarios

You can encapsulate this writing as a tool function for:

  • Filter user-submitted data;

  • Protect the security fields of the system configuration;

  • Controls the output content of the interface response structure.

summary

Although PHP provides a variety of methods to manipulate arrays, the flexibility of using functions such as array_diff_ukey() allows us to implement common demand logic in different ways. If you want to create a more robust and reusable array filter, you might as well try this idea!