Current Location: Home> Latest Articles> Comparison of parameters before and after simulation API request

Comparison of parameters before and after simulation API request

M66 2025-05-17

When developing APIs, there may be a requirement to compare requested parameters with expected parameters to see if they have changed. The array_diff_assoc function provided by PHP can help us achieve this. This function is used to compare key-value pairs of two arrays, returning different parts between them. Next, we will introduce how to use this function to simulate the comparison of parameters before and after API requests.

1. Basic usage of array_diff_assoc function

First, we need to understand the basic usage of the array_diff_assoc function. This function is used to compare two arrays and returns an array containing key-value pairs that exist in the first array but not in the second array.

Function prototype:

 array_diff_assoc(array $array1, array $array2): array
  • $array1 : The first array (the array to be compared).

  • $array2 : The second array (the array to be compared with the first array).

This function compares the key names and key values ​​of the array, and returns some key-value pairs in the first array, but there is no part of the second array with different values.

2. Comparison of parameters before and after simulation API request

Suppose we want to simulate parameter comparison before and after API request, we can usually use array_diff_assoc to help us find out the difference between the request parameters and the expected parameters. For example:

Scene:

We have an API request where the user submits the parameters. We want to compare the submitted parameters with the expected parameters to see if the user submits additional parameters or modifys the values ​​of some parameters.

Sample code:

 <?php
// Parameters before simulation request
$expectedParams = [
    'user_id' => 123,
    'name' => 'John Doe',
    'email' => 'john.doe@m66.net',
    'phone' => '123-456-7890'
];

// Simulate user-submitted parameters
$submittedParams = [
    'user_id' => 123,
    'name' => 'John Doe',
    'email' => 'john.doe@m66.net',
    'phone' => '987-654-3210',  // Different mobile phone numbers
    'address' => '123 Main St'  // Additional parameters were submitted
];

// use array_diff_assoc Comparison of two arrays
$diff = array_diff_assoc($submittedParams, $expectedParams);

// Output comparison results
if (!empty($diff)) {
    echo "The difference between the requested parameters and the expected parameters is as follows:\n";
    print_r($diff);
} else {
    echo "The requested parameters are consistent with the expected parameters。\n";
}
?>

Code explanation:

  1. $expectedParams : This is the request parameter we expect, including four fields: user_id , name , email and phone .

  2. $submittedParams : This is the request parameter submitted by the user. In addition to the normal four fields, an additional address field is submitted and the phone field is modified.

  3. Use array_diff_assoc to compare submittedParams and expectedParams and find out the differences between them.

  4. If there is a difference, use print_r to output the difference part; if there is no difference, output a consistent message.

Output:

 The difference between the requested parameters and the expected parameters is as follows:
Array
(
    [phone] => 987-654-3210
    [address] => 123 Main St
)

As shown above, the output shows the difference in user requests. The phone parameter has been modified, and address is an unnecessary parameter.

3. Advanced usage

Sometimes we may not only need to compare the differences between the two arrays, but also require more complex checks. For example, we might need to see if there are specific parameters missing, or if certain key fields meet a specific format. To enhance functionality, you can handle it in conjunction with other PHP functions.

Example: Missing parameter check

 <?php
$missingParams = array_diff_key($expectedParams, $submittedParams);
if (!empty($missingParams)) {
    echo "The following parameters are missing:\n";
    print_r($missingParams);
} else {
    echo "All expected parameters have been submitted。\n";
}
?>

In this example, array_diff_key is used to compare key names to find out which expected parameters are missing in the request.