Current Location: Home> Latest Articles> Quickly compare the differences between two objects' attributes using array_diff_key()

Quickly compare the differences between two objects' attributes using array_diff_key()

M66 2025-06-06

In PHP, array_diff_key() is a very useful function that can be used to compare key differences between two arrays or objects. If you have two objects and want to quickly compare their attribute differences, you can convert the objects into an array and then implement them through the array_diff_key() function. The following will use an example to explain in detail how to use array_diff_key() to compare the attribute differences of objects.

1. Understand the array_diff_key() function

The array_diff_key() function is used to compare the keys of two arrays and returns an array containing all keys that exist in the first array but do not exist in the second array.

Function definition:

 array_diff_key(array $array1, array $array2): array
  • $array1 : The first array.

  • $array2 : The second array.

Returns an array containing keys in $array1 but not in $array2 .

2. Compare object attribute differences

Because PHP objects are defined by classes, they do not have key names like arrays. To compare the property differences between two objects using array_diff_key() , you first need to convert the object into an array. This transformation can be achieved using the get_object_vars() function.

 <?php
class Person {
    public $name;
    public $age;
    public $email;

    public function __construct($name, $age, $email) {
        $this->name = $name;
        $this->age = $age;
        $this->email = $email;
    }
}

$obj1 = new Person("Alice", 25, "alice@m66.net");
$obj2 = new Person("Bob", 30, "bob@m66.net");

$array1 = get_object_vars($obj1);
$array2 = get_object_vars($obj2);

// use array_diff_key Compare the property differences of objects
$diff = array_diff_key($array1, $array2);

// Output difference
print_r($diff);
?>

Output:

 Array
(
    [name] => Alice
    [age] => 25
    [email] => alice@m66.net
)

3. Explain the code

  1. Definition class Person : We define a Person class that contains three public properties: name , age , and email .

  2. Create object : Two Person objects obj1 and obj2 are created, representing different personnel information respectively.

  3. Convert to array : convert these two objects into associative arrays through the get_object_vars() function.

  4. Comparison Difference : Use array_diff_key() to compare the key differences between two arrays. At this point, array_diff_key() will return the attribute key that is in $array1 but not in $array2 .

4. Results analysis

In our example, since the properties of obj1 and obj2 are exactly the same, array_diff_key() returns an empty array. You can adjust the attributes of the object according to actual business needs, so that array_diff_key() can display different attributes.

For example, suppose we modify obj2 so that it has no email attribute:

 $obj2 = new Person("Bob", 30, "");

At this time, executing array_diff_key() will find that obj1 has an email attribute, while obj2 does not.

5. Summary

array_diff_key() is a very effective tool that can help us quickly compare key differences between two arrays or objects. In actual development, especially when processing user data, API requests and other scenarios, it can be used very conveniently to find the differences in object properties. By converting objects into arrays, array_diff_key() can easily identify differences.

Hopefully this article can help you better understand and use the array_diff_key() function to compare the attribute differences between the two objects.