Comparing two PHP objects - PHP and OBJECTS

血红的双手。 提交于 2021-02-11 06:46:14

问题


I have two objects like this.

$array1

stdClass Object ( 
  [BellId] => 2 
  [BellCode] => BP001 
  [BellDescription] => SPI SPEED ABNORMAL,CHK BELT 
  [ControllerId] => 3 
  [CreatedBy] => 1 
  [CreatedOn] => 2016-08-19 15:09:25 
  [ModifiedBy] => 
  [ModifiedOn] => 
)

$array2

stdClass Object ( 
  [BellId] => 1 
  [BellCode] => BP002 
  [BellDescription] => MCB TRIPPED,CHK MTR SHORT,O/L. 
  [ControllerId] => 3 
  [CreatedBy] => 1 
  [CreatedOn] => 2016-08-19 15:09:25 
  [ModifiedBy] => 
  [ModifiedOn] => 
) 

I need to compare this object and get the difference in these two objects only.

I have checked the below links but no use.

Comparing two stdClass Objects

Comparing 2 objects PHP

My Sample code is as follows

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; 
}

Can someone help me with the code.


回答1:


Use array_diff_assoc(); e.g:

<?php

$foo = new stdClass();
$foo->BellId = 1;
$foo->BellDescription = 'foo';
$foo->CreatedBy = 1;

$bar = new stdClass();
$bar->BellId = 2;
$bar->BellDescription = 'bar';
$bar->CreatedBy = 1;

$diff = array_diff_assoc((array) $foo, (array) $bar);

print_r($diff);

array_diff_assoc performs a diff of arrays with additional index check. In your case this is required because you want to perform a key/value diff, not a diff on the values alone.

The above code yields:

Array
(
    [BellId] => 1
    [BellDescription] => foo
)

Note: you can transparently cast an instance of stdClass() to an array and vice versa:

$arr = ['id' => 1];
$obj = (object) $arr;
$arr = (array) $obj;

// etc. 

Hope this helps :)




回答2:


First convert both objects to arrays:

$arrayA = (array)$objectA;
$arrayB = (array)$objectB;

then just use array_diff to get the difference between arrays

$difference = array_diff($arrayA, $arrayB);

This will return an array containing keys from the first array ($arrayA) and their value, which gives an indication as to what fields are different between the two objects

foreach($difference as $key => $diff) {
    echo $objectA->$key;
    echo $objectB->$key;

    // the above two values will be different
}

Note: array_diff can be used if you know the order of fields in both objects are the same, however it's probably best to use array_diff_assoc here, as this offers an additional index check.




回答3:


With array_udiff_assoc you can compare the items as you like using a callback. Of course, you need to cast the objects to arrays:

$d = array_udiff_assoc((array)$array1, (array)$array2, function ($x, $y) {
  if (! (is_scalar($x) && is_scalar($y))) {
    trigger_error("skipping non-scalar members!", E_USER_WARNING);
    // you might want to handle this in the app-specific way
  }

  if (is_numeric($x) && is_numeric($y))
    return $x - $y;

  return strcmp($x, $y);
});
var_dump($d);

where $x and $y are the items from the arrays being compared.

Sample output

array(3) {
  ["BellId"]=>
  int(2)
  ["BellCode"]=>
  string(5) "BP001"
  ["BellDescription"]=>
  string(27) "SPI SPEED ABNORMAL,CHK BELT"
}

This is a very flexible way. You can put your own comparison logic into the callback. For instance, you might want to compare instances of classes:

  static $date_fmt = 'YmdHis';

  if ($x instanceof DateTime)
    $x = $x->format($date_fmt);
  if ($y instanceof DateTime)
    $y = $y->format($date_fmt);


来源:https://stackoverflow.com/questions/40171976/comparing-two-php-objects-php-and-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!