When developing APIs, we often need to compare the contents of two payloads in order to determine their differences. For example, when updating user information, compare the differences between new and old data, or when debugging the interface, check whether the returned data is consistent with the expected data. PHP provides a very useful function - array_diff_assoc , which can help us compare the differences between two arrays. Here is a detailed explanation of how to use this function to compare the payload content.
array_diff_assoc is a built-in function in PHP that compares key-value pairs of two arrays to find elements that exist in the first array but not in the second array. Unlike array_diff , array_diff_assoc not only compares values, but also compares key names. The element is considered different only when the key name and value do not match.
array_diff_assoc(array $array1, array $array2): array
$array1 : The first array, as a reference array for comparison.
$array2 : The second array, compared with the first array.
This function returns an array containing elements that exist in $array1 but not in $array2 .
Suppose we have two payloads in JSON format, representing the old and new user data, respectively, in the format as follows:
{
"user_id": 12345,
"name": "Zhang San",
"email": "zhangsan@m66.net",
"phone": "1234567890"
}
{
"user_id": 12345,
"name": "Zhang San",
"email": "zhangsan@m66.net",
"phone": "0987654321"
}
We want to compare the data differences in these two JSON payloads and check whether their key-value pairs are consistent. Here, we can use array_diff_assoc to implement it.
First, we need to convert these two JSON data into PHP arrays before we can compare them.
<?php
// Old data
$old_payload = '{"user_id": 12345, "name": "Zhang San", "email": "zhangsan@m66.net", "phone": "1234567890"}';
// New data
$new_payload = '{"user_id": 12345, "name": "Zhang San", "email": "zhangsan@m66.net", "phone": "0987654321"}';
// decoding JSON The data is PHP Array
$old_data = json_decode($old_payload, true);
$new_data = json_decode($new_payload, true);
// 输出Array查看结果
var_dump($old_data);
var_dump($new_data);
?>
With the array_diff_assoc function, we can easily find the difference between two arrays.
<?php
// Get the difference
$differences = array_diff_assoc($old_data, $new_data);
// Output difference
if (!empty($differences)) {
echo "Data Differences:\n";
print_r($differences);
} else {
echo "Data consistent,No difference。\n";
}
?>
Data Differences:
Array
(
[phone] => 1234567890
)
As shown above, the result shows that the value of the phone is different in the old data and the new data, showing the phone value of 1234567890 in the old data. This is exactly the difference we hope to get through comparison.
API debugging : When developing an API, you may need to compare the data returned by the interface with the expected results. Use array_diff_assoc to quickly view the differences and help developers locate problems.
Data update : In the scenario of user profile update, this method can be used to compare the differences between new and old data to determine which fields have changed.
Logging : When recording user behavior or data change logs, understand the specific content of data changes, so as to facilitate subsequent analysis.