How to compare by first converting an array with array_map() and then using array_diff_uassoc()?
In PHP, array_map() and array_diff_uassoc() are two very useful functions that can help us process and compare arrays in different scenarios. This article will introduce how to convert arrays using array_map() first, then use array_diff_uassoc() for comparison, and give specific examples.
The array_map() function can convert each element of an array through a callback function. The basic syntax is as follows:
array_map(callable $callback, array $array1, array ...$arrays);
$callback : A callback function used to process array elements.
$array1, ...$arrays : One or more input arrays.
array_map() returns a new array where each element is a value processed by the $callback function.
The array_diff_uassoc() function is used to compare two or more arrays, returning elements in the first array with neither key names nor values compared to other arrays. Unlike the regular array_diff() , array_diff_uassoc() allows us to provide a custom comparison function to compare key names.
The basic syntax is as follows:
array_diff_uassoc(array $array1, array $array2, callable $key_compare_func);
$array1 : The first array to be compared.
$array2 : The second array to compare.
$key_compare_func : A callback function used to compare array key names.
Now let's take a look at a practical example, how to convert the value of an array using array_map() , and then use array_diff_uassoc() for comparison.
Suppose we have two arrays representing the prices and names of the two sets of products:
$products1 = [
101 => "Apple",
102 => "Banana",
103 => "Orange"
];
$products2 = [
101 => "Apple",
102 => "Grapes",
104 => "Mango"
];
// price
$prices1 = [
101 => 3,
102 => 2,
103 => 1
];
$prices2 = [
101 => 3,
102 => 2.5,
104 => 1.5
];
Suppose we want to convert the product name to capital letters and use array_diff_uassoc() to compare elements with different prices and same key names in two price arrays.
// Convert the product name to capital letters first
$products1 = array_map('strtoupper', $products1);
$products2 = array_map('strtoupper', $products2);
// use array_diff_uassoc 比较price数组(The key name is the same,price不同)
$price_diff = array_diff_uassoc($prices1, $prices2, function ($key1, $key2) {
return $key1 - $key2;
});
print_r($price_diff);
We use array_map('strtoupper', $products1) and array_map('strtoupper', $products2) to convert product names to uppercase letters. This way we can ensure that the comparison of names is not affected by upper and lower case.
array_diff_uassoc() is used to compare price arrays. We provide a simple callback function for comparing key names ( $key1 - $key2 ). If the key names of the two arrays are the same but the corresponding prices are different, array_diff_uassoc() will return these elements.
When running the above code, we get an output:
Array
(
[102] => 2
[104] => 1.5
)
In this example, the prices of product IDs 102 and 104 are different, so they are returned.
Summarize
This article describes how to use the array_map() and array_diff_uassoc() functions in PHP. We first use array_map() to convert elements in the array, and then compare elements with different key names and corresponding values in two arrays through array_diff_uassoc() . In this way, we can flexibly process and compare arrays to suit different needs.
Dividing lines between the tail and article content