19

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 ?!

tereško
  • 58,060
  • 25
  • 98
  • 150
nfo
  • 637
  • 2
  • 6
  • 19
  • Usually you handle this with a method in the class, like a `equals($object)` method. Maybe you can't modify the class ? – Matthieu Napoli May 06 '11 at 12:15
  • 1
    Oh, and when comparing properties containing objects, how do you consider that objects are equal ? This is kind of a recursive question... You can check if their properties are equal (recursive problem...) or if they are the same instance (stronger than equality). – Matthieu Napoli May 06 '11 at 12:17
  • 1
    is the code above the exact one you are testing? if so, then $obj1 and $obj2's prop are the same array(1,2). technically there really is no difference. – dragonjet May 07 '11 at 07:37
  • @dragonjet - correct, but array_diff still fails to produce a full depth diff even if they were different. See my answer below. – Pete Hamilton May 07 '11 at 16:45
  • another problem is, even if array_diff goes recursively, for me it looks like, the type casting of object to array don´t.. – nfo May 09 '11 at 07:55
  • i have to correct myself, type casting of object to array works – nfo Jun 04 '12 at 11:17

2 Answers2

11

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
            )
    )
*/
Pete Hamilton
  • 7,730
  • 6
  • 33
  • 58
  • this is almost what i wont, it does not work if $obj->prop is another object with an array as property .. but i will keep that as best answer.. – nfo May 09 '11 at 07:06
  • In that case you could add into your loop something which checks to see if the object is not an array, whether you can cast it as one and run recursive_array_diff on it. – Pete Hamilton May 09 '11 at 12:02
  • Something like if (is_array($v)) { ... } elseif (is_array((array) $v) { ... } else { ...} You might need to check the number of elements in the cast array to check you're not accidentally casting values. Let me know if you work something out :) – Pete Hamilton May 09 '11 at 12:03
2

My solution will recursively diff a stdClass and all it nested arrays and stdClass objects. It is meant to be used for comparison of rest api responses.

function objDiff($obj1, $obj2):array { 
    $a1 = (array)$obj1;
    $a2 = (array)$obj2;
    return arrDiff($a1, $a2);
}

function arrDiff(array $a1, array $a2):array {
    $r = array();
    foreach ($a1 as $k => $v) {
        if (array_key_exists($k, $a2)) { 
            if ($v instanceof stdClass) { 
                $rad = objDiff($v, $a2[$k]); 
                if (count($rad)) { $r[$k] = $rad; } 
            }else if (is_array($v)){
                $rad = arrDiff($v, $a2[$k]);  
                if (count($rad)) { $r[$k] = $rad; } 
            // required to avoid rounding errors due to the 
            // conversion from string representation to double
            } else if (is_double($v)){ 
                if (abs($v - $a2[$k]) > 0.000000000001) { 
                    $r[$k] = array($v, $a2[$k]); 
                }
            } else { 
                if ($v != $a2[$k]) { 
                    $r[$k] = array($v, $a2[$k]); 
                }
            }
        } else { 
            $r[$k] = array($v, null); 
        } 
    } 
    return $r;     
} 

Here is a comparison function that I built using the pattern:

function objEq(stdClass $obj1, stdClass $obj2):bool { 
    $a1 = (array)$obj1;
    $a2 = (array)$obj2;
    return arrEq($a1, $a2);
}

function arrEq(array $a1, array $a2):bool {
    foreach ($a1 as $k => $v) {
        if (array_key_exists($k, $a2)) { 
            if ($v instanceof stdClass) { 
                $r = objEq($v, $a2[$k]); 
                if ($r === false) return false; 
            }else if (is_array($v)){
                $r = arrEq($v, $a2[$k]);  
                if ($r === false) return false; 
            } else if (is_double($v)){ 
            // required to avoid rounding errors due to the 
            // conversion from string representation to double
                if (abs($v - $a2[$k]) > 0.000000000001) { 
                    return false; 
                }
            } else { 
                if ($v != $a2[$k]) { 
                    return false; 
                }
            }
        } else { 
            return false; 
        } 
    } 
    return true;     
}

Usage:

$apiResponse = apiCall(GET, $objId);
$responseObj = json_decode($apiResponse);

// do stuff ...

if(!objEq($myObj, $responseObj) apiCall(PUT, $myObj, $objId);

Note that the apiCall function is just a mock to illustrate the concept. Also this solution is incomplete because it does not take into account any key->value pairs that are unique to obj2. In my use case this is not required and could be neglected.

NB: I borrowed heavily from Peter Hamiltons contribution. If you like what I did then please upvote his solution. Thanks!

FooBar
  • 5,752
  • 10
  • 44
  • 93
Soundbytes
  • 1,149
  • 9
  • 17