Current Location: Home> Latest Articles> In combination with array_map() for key filtering and conversion

In combination with array_map() for key filtering and conversion

M66 2025-05-15

How to use array_diff_ukey() and array_map() in combination to filter and convert keys?

In PHP, array_diff_ukey() and array_map() are very commonly used array functions, which are used to compare array keys and filter, and to map and convert arrays respectively. This article will introduce how to use these two functions in combination to implement filtering and conversion of array keys.

array_diff_ukey() function

array_diff_ukey() is used to compare keys of two or more arrays and return an array containing elements with different keys. The syntax of this function is as follows:

 array_diff_ukey(array $array1, array $array2, callable $key_compare_func): array
  • $array1 and $array2 are two arrays to be compared.

  • $key_compare_func is a callback function that compares keys in two arrays.

This function determines how to compare keys through the callback function. If the callback function returns 0, it means that the key is the same; it returns a negative value, it means that the key in $array1 is smaller than the key in $array2 ; it returns a positive value, it means that the key in $array1 is larger than the key in $array2 .

array_map() function

array_map() is used to apply a callback function to each element of the array and return a new array. The syntax is as follows:

 array_map(callable $callback, array $array): array
  • $callback is a callback function applied to each element.

  • $array is the array to be mapped.

Use array_diff_ukey() and array_map() in combination

We can use array_diff_ukey() to filter the keys, and then convert the values ​​of the array through array_map() . Here is a practical example that demonstrates how to use these two functions in combination to filter and convert keys.

 <?php
// Original array
$array1 = [
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'd' => 4
];

// Compare arrays,Suppose we want to keep 'a' and 'c' These two keys
$array2 = [
    'a' => 10,
    'c' => 20
];

// use array_diff_ukey Filter out not $array2 Keys present in
$filteredArray = array_diff_ukey($array1, $array2, function($key1, $key2) {
    return $key1 <=> $key2; // Compare the size of the key
});

// use array_map Convert values ​​to filtered arrays,Here we multiply the value by 10
$transformedArray = array_map(function($value) {
    return $value * 10;
}, $filteredArray);

// Output result
print_r($transformedArray);
?>

Code description

  1. We first define a raw array $array1 and an array $array2 for comparison.

  2. Through the array_diff_ukey() function, we compare the keys of $array1 and $array2 and filter out those keys in $array1 that are not in $array2 . In this example, the filtered array $filteredArray will contain the elements corresponding to the keys 'a' and 'c' .

  3. Then we use array_map() to convert the value of the $filteredArray array. In this example, we multiply the value by 10 to generate a new array $transformedArray .

  4. Finally, we output the converted array.

in conclusion

Through the combined use of array_diff_ukey() and array_map() , we can implement filtering array keys and converting their values. Such combinations allow for the flexibility of handling complex data operations, especially when both array keys and values ​​are required.