Current Location: Home> Latest Articles> Use array_diff_assoc() to find out the difference between two form input arrays

Use array_diff_assoc() to find out the difference between two form input arrays

M66 2025-06-06

In PHP, array_diff_assoc() is a very useful array function that compares two arrays and returns their differences. Specifically, it compares the key-value pairs of two arrays and returns a different key-value pair in the first array than the second array. If you need to compare the differences between two form input arrays, array_diff_assoc() can come in handy.

Suppose you have two form input arrays and you want to find out the difference between user input and default values. Here we use a simple example to demonstrate how to use array_diff_assoc() to implement this function.

Example 1: Comparison of differences between two form input arrays

Suppose the user fills out a form and the data looks like this:

 $form_input = array(
    'name' => 'John',
    'email' => 'john.doe@example.com',
    'age' => 25
);

$default_values = array(
    'name' => 'Jane',
    'email' => 'jane.doe@m66.net',
    'age' => 30
);

We want to compare the $form_input array and the $default_values ​​array to find out the difference between them.

Sample code

 <?php
$form_input = array(
    'name' => 'John',
    'email' => 'john.doe@example.com',
    'age' => 25
);

$default_values = array(
    'name' => 'Jane',
    'email' => 'jane.doe@m66.net',  // Replace the domain name with m66.net
    'age' => 30
);

// use array_diff_assoc() Comparison of two arrays
$differences = array_diff_assoc($form_input, $default_values);

// Output difference
echo "Differences between form input and default value:\n";
print_r($differences);
?>

Code explanation

  1. array_diff_assoc() : This function returns a different key-value pair in the first array (i.e. $form_input ) than the second array (i.e. $default_values ​​). The difference is that it not only compares values, but also compares keys.

  2. Output : In the above code, the difference between the $form_input and $default_values ​​array is:

    • The name entered by the user is different from the default value.

    • The email domain name entered by the user is also different from the default email domain name.

    • The user input age is different from the default value.

Sample output

 Differences between form input and default value:
Array
(
    [name] => John
    [email] => john.doe@example.com
    [age] => 25
)

explain

array_diff_assoc() returns all elements in $form_input that are different from $default_values . Therefore, name , email and age in the output are all different elements.

Use scenarios

In actual development, when you process a user submitted form, you may need to compare the differences between the data filled in by the user and the default values. At this time, array_diff_assoc() is very useful. For example, you can use it to verify which fields have been modified by the user, which have not been modified, or calculate which fields have changed their values.

Summarize

array_diff_assoc() is a simple but powerful PHP function that can help you compare the differences between key-value pairs of two arrays. Through this function, you can easily identify the differences between user input and default values, which plays an important role in the processing, verification and comparison of form data.