Current Location: Home> Latest Articles> Use array_diff_uassoc() to compare the differences in object-to-array

Use array_diff_uassoc() to compare the differences in object-to-array

M66 2025-05-15

How to compare the difference between an object after turning into an array using array_diff_uassoc()?

In PHP, array_diff_uassoc() is a very useful function that compares two arrays and judges the difference based on key-value callback functions. This function works very well if you have an object and want to convert it into an array to compare the differences.

1. Basic usage of array_diff_uassoc()

array_diff_uassoc() is used to compare the differences between key-value pairs of two arrays and use a user-defined callback function to determine how the differences are compared. The syntax is as follows:

 array_diff_uassoc(array $array1, array $array2, callable $key_compare_func) : array
  • array1 and array2 : These two arrays are the arrays to be compared.

  • key_compare_func : This is a callback function used to compare keys of an array.

This function returns an array containing differences, where each difference term comes from the first array ( array1 ) and does not appear in the second array ( array2 ).

2. Example: How to compare the difference after object is converted to array using array_diff_uassoc()

Suppose you have two objects and you want to compare the differences between them after converting the objects into an array. Let's first define two simple classes:

 class Product {
    public $id;
    public $name;
    public $price;

    public function __construct($id, $name, $price) {
        $this->id = $id;
        $this->name = $name;
        $this->price = $price;
    }
}

$product1 = new Product(1, "Apple", 100);
$product2 = new Product(2, "Banana", 50);
$product3 = new Product(3, "Cherry", 75);

// Convert objects to arrays
$array1 = (array)$product1;
$array2 = (array)$product2;
$array3 = (array)$product3;

After converting the object into an array, we can use array_diff_uassoc() to compare the differences between these arrays.

3. Compare the differences after objects are converted to arrays

We define a callback function to compare the keys of two arrays. In this example, we simply compare the key names:

 function compare_keys($key1, $key2) {
    return strcmp($key1, $key2);
}

// use array_diff_uassoc() Comparison of the differences between two arrays
$diff1 = array_diff_uassoc($array1, $array2, 'compare_keys');
$diff2 = array_diff_uassoc($array1, $array3, 'compare_keys');

// Output result
print_r($diff1);
print_r($diff2);

In this example, array_diff_uassoc() will compare $array1 and $array2 , $array1 and $array3 , and return their differences.

4. Output result

The difference in output will show the parts of the two arrays with different keys and values. You can adjust the callback function according to actual needs to meet specific comparison needs.

5. Why use array_diff_uassoc() ?

The advantage of using array_diff_uassoc() is that you can not only compare the values ​​of the array, but also compare the keys of the array according to custom rules. This is very useful for comparing the differences in the arrayed properties of objects, especially when the data structure is relatively complex.

Summarize

By converting the object into an array and using array_diff_uassoc() , we can easily compare the property differences between the two objects. array_diff_uassoc() provides a flexible callback mechanism, making it suitable not only for regular array comparisons, but also for customized comparisons based on special rules of keys.