Current Location: Home> Latest Articles> Can the array_diff() function in PHP handle object arrays? How to compare array of objects using array_diff()?

Can the array_diff() function in PHP handle object arrays? How to compare array of objects using array_diff()?

M66 2025-05-14

In PHP, the array_diff() function is used to compare two or more arrays, returning elements that exist in the first array but not in other arrays. Usually, it is used to handle simple arrays, but the problem becomes slightly more complicated when we encounter arrays of objects. So, can array_diff() handle object arrays? If not, how to implement similar functions? This article will introduce it in detail.

1. Basic usage of array_diff() function

The basic usage of the array_diff() function is to compare elements between arrays and return those that only appear in the first array. Its function signature is as follows:

 array_diff(array $array1, array $array2, array ...$arrays): array

This function takes two or more arrays as parameters and returns the unique elements in the first array. Here is a simple example:

 $array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];

$result = array_diff($array1, $array2);
print_r($result);

Output:

 Array
(
    [0] => 1
    [1] => 2
)

2. Can array_diff() handle object arrays?

The array_diff() function works based on "value comparison", which means it uses the == operator to compare elements in an array. For object arrays, array_diff() cannot be compared directly like normal arrays. Objects are reference types in PHP, so array_diff() considers them to be different objects even if the attribute values ​​of objects are the same.

For example, suppose we have two arrays of objects with the same content, but they are different object instances:

 class Person {
    public $name;
    public $age;

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

$obj1 = new Person('John', 30);
$obj2 = new Person('John', 30);
$obj3 = new Person('Jane', 25);

$array1 = [$obj1, $obj2];
$array2 = [$obj2, $obj3];

$result = array_diff($array1, $array2);
print_r($result);

Output:

 Array
(
    [0] => Person Object
        (
            [name] => John
            [age] => 30
        )
)

In this example, although the attribute values ​​of $obj1 and $obj2 are the same, array_diff() thinks they are different because they are different object instances, so $obj1 is returned without any object with the same attribute value.

3. How to use array_diff() to compare object arrays?

To compare arrays of objects using array_diff() , we can solve this problem by implementing custom comparison rules. A common method is to use the array_map() function to convert objects into arrays and compare their attribute values.

Here is a solution based on array_map() , which compares by converting objects into attribute arrays:

 class Person {
    public $name;
    public $age;

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

    // Convert to an array,Convenient comparison
    public function toArray() {
        return ['name' => $this->name, 'age' => $this->age];
    }
}

$obj1 = new Person('John', 30);
$obj2 = new Person('John', 30);
$obj3 = new Person('Jane', 25);

$array1 = [$obj1, $obj2];
$array2 = [$obj2, $obj3];

// usearray_map将对象Convert to an array
$array1 = array_map(function($obj) {
    return $obj->toArray();
}, $array1);

$array2 = array_map(function($obj) {
    return $obj->toArray();
}, $array2);

// usearray_diffMake a comparison
$result = array_diff($array1, $array2);
print_r($result);

Output:

 Array
(
    [0] => Array
        (
            [name] => John
            [age] => 30
        )
)

In this example, we add a toArray() method to the Person class to convert the properties of the object into an array. Then, use array_map() to convert the object array into an attribute array and use array_diff() to compare. This way you can compare object arrays by value.

4. Conclusion

The array_diff() function cannot process the object array directly because it is based on value comparisons, and objects are reference types in PHP. If you need to compare arrays of objects, you can convert objects into arrays by custom conversion rules and compare their attribute values. The array_map() and toArray() methods are a common solution.

In this way, we can flexibly compare object arrays and implement functions similar to array_diff() .