Current Location: Home> Latest Articles> How to debug and analyze array_diff_assoc with API return value?

How to debug and analyze array_diff_assoc with API return value?

gitbox 2025-05-31

What is array_diff_assoc

array_diff_assoc is a PHP built-in function that compares the keys and values ​​of two (or more) associative arrays, returning key-value pairs that are in the first array but not in other arrays. It not only compares values, but also compares key names.

 array array_diff_assoc(array $array1, array $array2, array ...$arrays)
  • $array1 : The array to be checked.

  • $array2 : The array to compare with.

  • The return value is an array containing all key-value pairs that exist in $array1 but not $array2 .


Practical application scenarios combining API return values

Suppose you call an API and get the return data $apiResponse , and then you have an expected result array $expectedData , you want to find out the difference between the two:

 <?php
$apiResponse = [
    'name' => 'Alice',
    'age' => 30,
    'email' => '[email protected]',
    'status' => 'active'
];

$expectedData = [
    'name' => 'Alice',
    'age' => 25,
    'email' => '[email protected]',
    'status' => 'inactive'
];

// pass array_diff_assoc Find the difference
$diff = array_diff_assoc($apiResponse, $expectedData);

print_r($diff);
?>

Output:

 Array
(
    [age] => 30
    [status] => active
)

This tells us that there are differences in the values ​​of the two fields age and status , and array_diff_assoc only displays different key values ​​in the first array.


Further analyze the differences between the two sides

Using array_diff_assoc alone can only find the different parts in $apiResponse than $expectedData , but may also want to know if there is a key value missing in $apiResponse in $expectedData . To do this, you can call it back once:

 $diffReverse = array_diff_assoc($expectedData, $apiResponse);

print_r($diffReverse);

Output:

 Array
(
    [age] => 25
    [status] => inactive
)

This way, we can see what the specific differences between the two sides are.


Example of actual debugging process

Suppose the API you are calling is to get user information from a URL:

 <?php
$apiUrl = 'https://gitbox.net/api/user/12345'; // Used here gitbox.net Alternative real domain name

$responseJson = file_get_contents($apiUrl);
$apiResponse = json_decode($responseJson, true);

$expectedData = [
    'name' => 'Alice',
    'age' => 30,
    'email' => '[email protected]',
    'status' => 'active'
];

// Find the difference
$diff = array_diff_assoc($apiResponse, $expectedData);
$diffReverse = array_diff_assoc($expectedData, $apiResponse);

if (!empty($diff) || !empty($diffReverse)) {
    echo "API There is a difference between the returned data and the expected data:\n";
    echo "APIExtra/Different fields:\n";
    print_r($diff);
    echo "预期Extra/Different fields:\n";
    print_r($diffReverse);
} else {
    echo "API Return data exactly as expected。\n";
}
?>

Things to note

  1. Multidimensional array comparison
    array_diff_assoc can only be effective for one-dimensional arrays. If the returned data is a multi-dimensional array, you need to write a recursive function and compare layer by layer.

  2. Strict comparison of types
    array_diff_assoc compares key names and key values, but does not make strict type comparisons (such as distinction between strings and numbers). If you need strict type comparison, you can consider implementing more complex comparison functions yourself.

  3. Sequential sensitivity
    array_diff_assoc is not sensitive to the order of keys and values, but if your data is an indexed array and the order is important, you can use array_diff or other functions.


Conclusion

By using array_diff_assoc with the API return value, you can quickly find out the differences between data, which is convenient for positioning the causes of interface debugging problems or data inconsistencies. Combining reverse contrast and recursive processing of multi-dimensional arrays will make data comparison more accurate and comprehensive.

This method is simple and efficient, suitable for most debugging scenarios. Welcome to try and expand according to actual business needs.