In web development, it's common to compare submitted form data with the original dataset, especially in scenarios where a user updates their profile information. For instance, when a user modifies their personal details, we often need to identify which fields were changed. PHP provides the array_diff_assoc() function, which can help us accomplish this task effortlessly. Today, we’ll walk through a real-world example to learn how to use this function to detect modified fields in a form.
The array_diff_assoc() function is used to compute the difference between two arrays, comparing both the keys and the values. It returns an array containing elements from the first array that are not present in the second array. It's important to note that this function compares not just the values but also the keys, making it stricter than array_diff().
Function prototype:
<span><span><span class="hljs-title function_ invoke__">array_diff_assoc</span></span><span>(</span><span><span class="hljs-keyword">array</span></span><span> </span><span><span class="hljs-variable">$array1</span></span><span>, </span><span><span class="hljs-keyword">array</span></span><span> </span><span><span class="hljs-variable">$array2</span></span><span>): </span><span><span class="hljs-keyword">array</span></span><span>
</span></span>
$array1: The first array.
$array2: The second array.
Return value: An array containing all elements from $array1 that differ from $array2.
Let’s say we have a user profile update form where the submitted data includes fields like name, email, and phone number. We want to compare this with the original user data to identify which fields have been modified.
First, define one array with the original user data and another with the data submitted through the form.
<span><span><span class="hljs-comment">// Assume this is the original user data stored in the database</span></span><span>
</span><span><span class="hljs-variable">$original_data</span></span><span> = [
</span><span><span class="hljs-string">'name'</span></span><span> => </span><span><span class="hljs-string">'John Doe'</span></span><span>,
</span><span><span class="hljs-string">'email'</span></span><span> => </span><span><span class="hljs-string">'[email protected]'</span></span><span>,
</span><span><span class="hljs-string">'phone'</span></span><span> => </span><span><span class="hljs-string">'123-456-7890'</span></span><span>
];
<p></span>// Assume this is the modified data submitted by the user<br>
$submitted_data = [<br>
'name' => 'John Doe', // Name unchanged<br>
'email' => '<a class="cursor-pointer" rel="noopener">[email protected]</a>', // Email changed<br>
'phone' => '123-456-7890' // Phone unchanged<br>
];<br>
</span>
Next, use array_diff_assoc() to compare the original and submitted arrays to find out which fields were changed.
<span><span><span class="hljs-comment">// Use array_diff_assoc to detect changed fields</span></span><span>
</span><span><span class="hljs-variable">$changed_fields</span></span><span> = </span><span><span class="hljs-title function_ invoke__">array_diff_assoc</span></span><span>(</span><span><span class="hljs-variable">$submitted_data</span></span><span>, </span><span><span class="hljs-variable">$original_data</span></span><span>);
<p></span>// Output the changed fields<br>
if (!empty($changed_fields)) {<br>
echo "Modified fields:\n";<br>
foreach ($changed_fields as $key => $value) {<br>
echo "$key: $value\n";<br>
}<br>
} else {<br>
echo "No fields were modified.\n";<br>
}<br>
</span>
<span><span>Modified fields:
</span><span><span class="hljs-symbol">email</span></span><span><span class="hljs-punctuation">:</span></span><span> [email protected]
</span></span>
In this example, array_diff_assoc() compares the keys and values of both submitted_data and original_data.
Since the name and phone fields remained unchanged, only the email field is flagged as modified, which is why it appears in the output.
If you also want to detect whether a user submitted any new fields that didn’t exist in the original data, you can use a combination of array_diff_assoc() and array_diff_key().
<span><span><span class="hljs-comment">// Use array_diff_key to check for newly submitted fields</span></span><span>
</span><span><span class="hljs-variable">$new_fields</span></span><span> = </span><span><span class="hljs-title function_ invoke__">array_diff_key</span></span><span>(</span><span><span class="hljs-variable">$submitted_data</span></span><span>, </span><span><span class="hljs-variable">$original_data</span></span><span>);
<p></span>if (!empty($new_fields)) {<br>
echo "New fields submitted:\n";<br>
foreach ($new_fields as $key => $value) {<br>
echo "$key: $value\n";<br>
}<br>
}<br>
</span>
array_diff_assoc() is a very useful function that makes it easy to detect which fields have been changed in a submitted user form. It is especially helpful in scenarios like updating user profiles or modifying orders, ensuring that we can accurately track what has changed.
With the example above, you can quickly implement a comparison feature between original and submitted data, which will help you better manage user inputs and form validation.