获取两个平面对象之间的差异

问题描述 投票:0回答:3

我有两个这样的物体。

$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] => 
)

$数组2

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] => 
) 

我需要比较这个对象并仅获得这两个对象的差异。

我已检查以下链接,但没有用。

比较两个 stdClass 对象

比较 2 个对象 PHP

我的示例代码如下

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

有人可以帮我解决代码吗?

php arrays object comparison
3个回答
4
投票

使用

array_diff_assoc()
;例如:

<?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
通过附加索引检查执行数组差异。在您的情况下,这是必需的,因为您想要执行键/值差异,而不是单独对值进行差异。

上面的代码产生:

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

注意:您可以将

stdClass()
的实例透明地转换为
array
,反之亦然:

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

// etc. 

希望这有帮助:)


0
投票

首先将两个对象转换为数组:

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

然后使用 array_diff 来获取数组之间的差异

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

这将返回一个数组,其中包含第一个数组 ($arrayA) 中的键及其值,这表明两个对象之间哪些字段不同

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

    // the above two values will be different
}

注意:如果您知道两个对象中的字段顺序相同,则可以使用 array_diff,但是最好在这里使用

array_diff_assoc
,因为这提供了额外的索引检查。


0
投票

使用

array_udiff_assoc
,您可以使用回调来比较项目。当然,您需要将对象转换为数组:

$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);

其中

$x
$y
是正在比较的数组中的项目。

示例输出

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

这是一种非常灵活的方式。您可以将自己的比较逻辑放入回调中。例如,您可能想要比较类的实例:

  static $date_fmt = 'YmdHis';

  if ($x instanceof DateTime)
    $x = $x->format($date_fmt);
  if ($y instanceof DateTime)
    $y = $y->format($date_fmt);
© www.soinside.com 2019 - 2024. All rights reserved.