PHP综合对象差异函数
PHP Comprehensive Object Difference Function
有谁知道 php 函数可以接受两个对象并 return 将一整套差异作为一个对象返回?
我将使用 json 编码数据作为我要完成的示例:
对象 A:
{
"Name":"Original",
"Id": 5,
"Data":{
"Value1": 1,
"Value2": [
5, 7, 8, 10
],
"Value3": {
"Exists": true
}
}
}
对象 B:
{
"Name":"ThisNameChanged",
"Id": 5,
"Data":{
"Value1": 7,
"Value2": [
5, 8, 9
],
"Value3": {
"Exists": true
}
}
}
会 return:
{
"Name":"ThisNameChanged",
"Data":{
"Value2": {
"1": 8,
"2": 9
}
}
}
如果不存在这样的函数(我猜很有可能),我该如何编写一个函数来执行此操作?谁能提供一个例子或功能?
这是您要求的函数的一些简单示例,
但还不够完美,仅供大家深思:
function objDiff ($obj1, $obj2) {
$diff=array();
$obj1Arr = (array)$obj1;
$obj2Arr = (array)$obj2;
foreach ($obj1Arr as $key=>$val) {
if (!(isset($obj2Arr[$key]) && $obj2Arr[$key]===$val )) {
if (gettype($val)=='object') {
if (isset($obj2Arr[$key])) {
if (gettype($obj2Arr[$key])=='object') {
$subDiff = objDiff($val,$obj2Arr[$key]);
$diff[$key]=$subDiff;
} else {
$diff[$key]=array($val,$obj2Arr[$key]);
}
} else {
$diff[$key]=array($val,$obj2Arr[$key]);
}
} else {
$diff[$key]=array($val,(isset($obj2Arr[$key]))?$obj2Arr[$key]:null);
}
}
}
return $diff;
}
使用此数据:
$obj1 = new stdClass();
$obj1->test1 = 1;
$obj1->test2 = 2;
$subObj = new stdClass();
$subObj->test1 = 1;
$subObj->test2 = 2;
$obj1->test3 = $subObj;
$obj2 = new stdClass();
$obj2->test1 = 3;
$obj2->test2 = 4;
$subObj = new stdClass();
$subObj->test1 = 3;
$subObj->test2 = 2;
$obj2->test3 = $subObj;
print_r(objDiff($obj1,$obj2) );
它returns:
Array
(
[test1] => Array
(
[0] => 1
[1] => 3
)
[test2] => Array
(
[0] => 2
[1] => 4
)
[test3] => Array
(
[test1] => Array
(
[0] => 1
[1] => 3
)
)
)
感谢 Kim Alexander,我能够编写代码来完成我想要做的事情。
function objDiff($obj1, $obj2, $diff = null) {
if (is_null($diff))
$diff = new stdClass;
$obj1Arr = (array)$obj1;
$obj2Arr = (array)$obj2;
foreach($obj1Arr as $key => $val) {
if (isset($obj2Arr[$key]))
{
if ($obj2Arr[$key] != $val)
{
if (is_object($obj2Arr[$key]) && is_object($val))
$diff->$key = objDiff($val, $obj2Arr[$key]);
else
$diff->$key = $obj2Arr[$key];
}
}
}
return $diff;
}
有谁知道 php 函数可以接受两个对象并 return 将一整套差异作为一个对象返回?
我将使用 json 编码数据作为我要完成的示例:
对象 A:
{
"Name":"Original",
"Id": 5,
"Data":{
"Value1": 1,
"Value2": [
5, 7, 8, 10
],
"Value3": {
"Exists": true
}
}
}
对象 B:
{
"Name":"ThisNameChanged",
"Id": 5,
"Data":{
"Value1": 7,
"Value2": [
5, 8, 9
],
"Value3": {
"Exists": true
}
}
}
会 return:
{
"Name":"ThisNameChanged",
"Data":{
"Value2": {
"1": 8,
"2": 9
}
}
}
如果不存在这样的函数(我猜很有可能),我该如何编写一个函数来执行此操作?谁能提供一个例子或功能?
这是您要求的函数的一些简单示例,
但还不够完美,仅供大家深思:
function objDiff ($obj1, $obj2) {
$diff=array();
$obj1Arr = (array)$obj1;
$obj2Arr = (array)$obj2;
foreach ($obj1Arr as $key=>$val) {
if (!(isset($obj2Arr[$key]) && $obj2Arr[$key]===$val )) {
if (gettype($val)=='object') {
if (isset($obj2Arr[$key])) {
if (gettype($obj2Arr[$key])=='object') {
$subDiff = objDiff($val,$obj2Arr[$key]);
$diff[$key]=$subDiff;
} else {
$diff[$key]=array($val,$obj2Arr[$key]);
}
} else {
$diff[$key]=array($val,$obj2Arr[$key]);
}
} else {
$diff[$key]=array($val,(isset($obj2Arr[$key]))?$obj2Arr[$key]:null);
}
}
}
return $diff;
}
使用此数据:
$obj1 = new stdClass();
$obj1->test1 = 1;
$obj1->test2 = 2;
$subObj = new stdClass();
$subObj->test1 = 1;
$subObj->test2 = 2;
$obj1->test3 = $subObj;
$obj2 = new stdClass();
$obj2->test1 = 3;
$obj2->test2 = 4;
$subObj = new stdClass();
$subObj->test1 = 3;
$subObj->test2 = 2;
$obj2->test3 = $subObj;
print_r(objDiff($obj1,$obj2) );
它returns:
Array
(
[test1] => Array
(
[0] => 1
[1] => 3
)
[test2] => Array
(
[0] => 2
[1] => 4
)
[test3] => Array
(
[test1] => Array
(
[0] => 1
[1] => 3
)
)
)
感谢 Kim Alexander,我能够编写代码来完成我想要做的事情。
function objDiff($obj1, $obj2, $diff = null) {
if (is_null($diff))
$diff = new stdClass;
$obj1Arr = (array)$obj1;
$obj2Arr = (array)$obj2;
foreach($obj1Arr as $key => $val) {
if (isset($obj2Arr[$key]))
{
if ($obj2Arr[$key] != $val)
{
if (is_object($obj2Arr[$key]) && is_object($val))
$diff->$key = objDiff($val, $obj2Arr[$key]);
else
$diff->$key = $obj2Arr[$key];
}
}
}
return $diff;
}