Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare object properties and show diff in PHP

I'm searching for a way to show me the different properties/values from given objects...

$obj1 = new StdClass; $obj1->prop = 1;
$obj2 = new StdClass; $obj2->prop = 2;

var_dump(array_diff((array)$obj1, (array)$obj2));
//output array(1) { ["prop"]=> int(1) }

This works very well as long the property is not a object or array.

$obj1 = new StdClass; $obj1->prop = array(1,2);
$obj2 = new StdClass; $obj2->prop = array(1,3); 

var_dump(array_diff((array)$obj1, (array)$obj2))
// Output array(0) { }
// Expected output - array { ["prop"]=> array { [1]=> int(2) } }

Is there a way to get rid of this, even when the property is another object ?!

like image 733
nfo Avatar asked May 06 '11 11:05

nfo


People also ask

How do you compare two values of objects?

In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity.

Is a function to comparing the objects?

Comparing objects is easy, use === or Object.is(). This function returns true if they have the same reference and false if they do not.

When checking to see if two variables contain the same instance of an Object which of the following comparisons should be used?

When using the comparison operator ( == ), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values (values are compared with == ), and are instances of the same class.

How do you check if something equals in PHP?

PHP provides two comparison operators to check equality of two values. The main difference between of these two is that '==' checks if the values of the two operands are equal or not . On the other hand, '===' checks the values as well as the type of operands are equal or not . <?


1 Answers

Something like the following, which iterates through and does a recursive diff is the item in the array is itself an array could work:

Des similar work to array_diff, but it does a check to see if it is an array first (is_array) and if so, sets the diff for that key to be the diff for that array. Repeats recursively.

function recursive_array_diff($a1, $a2) { 
    $r = array(); 
    foreach ($a1 as $k => $v) {
        if (array_key_exists($k, $a2)) { 
            if (is_array($v)) { 
                $rad = recursive_array_diff($v, $a2[$k]); 
                if (count($rad)) { $r[$k] = $rad; } 
            } else { 
                if ($v != $a2[$k]) { 
                    $r[$k] = $v; 
                }
            }
        } else { 
            $r[$k] = $v; 
        } 
    } 
    return $r; 
}

It then works like this:

$obj1 = new StdClass; $obj1->prop = array(1,2);
$obj2 = new StdClass; $obj2->prop = array(1,3);
print_r(recursive_array_diff((array)$obj1, (array)$obj2));

/* Output:
    Array
    (
        [prop] => Array
            (
                [1] => 2
            )
    )
*/
like image 151
Pete Hamilton Avatar answered Sep 19 '22 16:09

Pete Hamilton