PHP获得两个对象数组的差异

时间:2022-10-20 22:25:53

I know there is array_diff and array_udiff for comparing the difference between two arrays, but how would I do it with two arrays of objects?

我知道有array_diff和array_udiff用于比较两个数组之间的差异,但是如何使用两个对象数组呢?

array(4) {
    [0]=>
        object(stdClass)#32 (9) {
            ["id"]=>
            string(3) "205"
            ["day_id"]=>
            string(2) "12"
        }
}

My arrays are like this one, I am interested to see the difference of two arrays based on IDs.

我的数组就像这个,我有兴趣看到基于ID的两个数组的区别。

3 个解决方案

#1


57  

This is exactly what array_udiff is for. Write a function that compares two objects the way you would like, then tell array_udiff to use that function. Something like this:

这正是array_udiff的用途。编写一个以您希望的方式比较两个对象的函数,然后告诉array_udiff使用该函数。像这样的东西:

function compare_objects($obj_a, $obj_b) {
  return $obj_a->id - $obj_b->id;
}

$diff = array_udiff($first_array, $second_array, 'compare_objects');

Or, if you're using PHP >= 5.3 you can just use an anonymous function instead of declaring a function:

或者,如果您使用PHP> = 5.3,则可以使用匿名函数而不是声明函数:

$diff = array_udiff($first_array, $second_array,
  function ($obj_a, $obj_b) {
    return $obj_a->id - $obj_b->id;
  }
);

#2


4  

Here's another option, if you want to run the diff according to object instances. You would use this as your callback to array_udiff:

如果要根据对象实例运行diff,这是另一个选项。你可以使用它作为你对array_udiff的回调:

function compare_objects($a, $b) {
    return strcmp(spl_object_hash($a), spl_object_hash($b));
}

You'd only want to use that if you're certain that the arrays both contain only objects - here's my personal use case.

如果您确定数组都只包含对象,那么您只想使用它 - 这是我的个人用例。

#3


1  

And here is another option if you wanna compare string properties (e.g. name):

如果你想比较字符串属性(例如名字),这里有另一个选项:

$diff = array_udiff($first_array, $second_array,
  function ($obj_a, $obj_b) {
    return strcmp($obj_a->name, $obj_b->name);
  }
);

#1


57  

This is exactly what array_udiff is for. Write a function that compares two objects the way you would like, then tell array_udiff to use that function. Something like this:

这正是array_udiff的用途。编写一个以您希望的方式比较两个对象的函数,然后告诉array_udiff使用该函数。像这样的东西:

function compare_objects($obj_a, $obj_b) {
  return $obj_a->id - $obj_b->id;
}

$diff = array_udiff($first_array, $second_array, 'compare_objects');

Or, if you're using PHP >= 5.3 you can just use an anonymous function instead of declaring a function:

或者,如果您使用PHP> = 5.3,则可以使用匿名函数而不是声明函数:

$diff = array_udiff($first_array, $second_array,
  function ($obj_a, $obj_b) {
    return $obj_a->id - $obj_b->id;
  }
);

#2


4  

Here's another option, if you want to run the diff according to object instances. You would use this as your callback to array_udiff:

如果要根据对象实例运行diff,这是另一个选项。你可以使用它作为你对array_udiff的回调:

function compare_objects($a, $b) {
    return strcmp(spl_object_hash($a), spl_object_hash($b));
}

You'd only want to use that if you're certain that the arrays both contain only objects - here's my personal use case.

如果您确定数组都只包含对象,那么您只想使用它 - 这是我的个人用例。

#3


1  

And here is another option if you wanna compare string properties (e.g. name):

如果你想比较字符串属性(例如名字),这里有另一个选项:

$diff = array_udiff($first_array, $second_array,
  function ($obj_a, $obj_b) {
    return strcmp($obj_a->name, $obj_b->name);
  }
);