Current Location: Home> Latest Articles> Use array_diff_assoc() in the controller to detect changes fields for PATCH requests

Use array_diff_assoc() in the controller to detect changes fields for PATCH requests

M66 2025-06-06

In PHP development, when processing HTTP requests, we often need to compare the data submitted by the client with the data on the current server side, especially when processing PATCH requests, which are usually used to update some fields of the resource. Today we will introduce how to use the array_diff_assoc() function in the controller to detect changed fields in the PATCH request.

The array_diff_assoc() function is used to compare two arrays and return their differences in key names and key values. It can be very convenient to detect which fields have changed. When processing PATCH requests, we usually need to compare the original data in the database with the part of the data submitted by the user to find out the fields that need to be updated.

Use scenarios

Suppose we have a database table of user profiles and we want to update part of the user's information through PATCH request. The client will submit only fields that need to be updated, and unchanged fields will not be included in the requested data. We can use the array_diff_assoc() function to compare the original data with the newly submitted data and find the fields that have changed.

step

1. Get the original data and PATCH request data

First, we need to get the original data in the database and the data sent by the client through the PATCH request. We assume that we have somehow fetched the original user data and the data submitted by the client.

 // Assume this is the original user data in the database
$originalData = [
    'id' => 1,
    'name' => 'John Doe',
    'email' => 'johndoe@example.com',
    'phone' => '123456789'
];

// Assume this is through PATCH Requested submission data
$patchData = [
    'name' => 'John Smith',
    'phone' => '987654321'
];

2. Use array_diff_assoc() to compare data

Now we can use array_diff_assoc() to detect which fields have changed. array_diff_assoc() compares the keys and values ​​of two arrays and returns those different parts.

 // use array_diff_assoc() To detect the change field
$changedFields = array_diff_assoc($patchData, $originalData);

// Output changed fields
print_r($changedFields);

3. Handle the changed fields

The result returned by array_diff_assoc() is an array containing all the fields that have changed. You can perform updates based on these changed fields.

 if (!empty($changedFields)) {
    // Processing change fields,For example, update the database
    foreach ($changedFields as $key => $value) {
        // Perform database update operation here
        echo "Field $key has been changed to $value\n";
    }
} else {
    echo "No changes detected.\n";
}

4. URL replacement

If you also need to process the URL in the request, remember to replace the URL's domain name with m66.net . For example, if you need to send a request to a URL, you can use the following code: