array intersect for object array php

前端 未结 8 1487
独厮守ぢ
独厮守ぢ 2021-01-04 05:55

I want to know how to array_intersect for object array.

相关标签:
8条回答
  • 2021-01-04 05:58

    Just for completeness: Implement __toString() method in your object returning a unique value. For database entities this might be as easy as returning the fully qualified class name postfixed with the ID of the record. But it can also be arbitrarily complex by doing some hashing or even worse things.

    In my opinion, it's the class's duty to serialize itself or create something unique to compare its objects by. Using anything outside of a class to serialize an object might result in strange behaviour (including comparing objects of different classes, which must never result in equality).

    0 讨论(0)
  • 2021-01-04 06:03

    I use array_udiff to implement array_intersect for an object array.

     function diff($a, $b) {
     if($a === $b) {
         return 0;
     } else {
         return 1;}
     }
    
     $array_1 = array('a', 'b', 'c');    
    
     $array_2 = array('c', 'd','e');    
    
     $array = array_udiff($array_1, array_udiff($array_1, $array_2, 'diff'),'diff');
    
    var_dump($array);
    return array(1) { [2]=> string(1) "c" }
    

    You can have your own diff function for any scheme.

    0 讨论(0)
  • 2021-01-04 06:07

    Had a similar problem a few days ago, while these are answers are on the right path; I used them to work out the following:

    From Artefacto's answer return $obj1 == $obj2 didn't really work, so I wrote a simple comparative function (basically gets the md5 of the serialised object and compares that):

    function object_compare($obj1, $obj2){
      $md5 = function($obj){
        return md5(serialize($obj));
      };
      return strcmp($md5($obj1), $md5($obj2));
    }
    

    Then it’s jut a matter of calling array_uintersect with our comparative function to get the intersection:

    # $array1 / $array2 are the array of objects we want to compare
    return array_uintersect($array1, $array2, 'object_compare');
    

    In my case, I had an unknown / dynamic array of objects, so I took it a step further so I don't have to declare array_uintersect($array1, $array2, ...) specifically - but just be able to pass in an array of arrays (of objects):

    # $multiarray_of_objects is our array of arrays
    $multiarray_of_objects[] = 'object_compare';
    return call_user_func_array('array_uintersect', $multiarray_of_objects);
    

    Just gotta remember to pass in the reference to our callback / comparative function as the last string in the array. Works like a charm!

    0 讨论(0)
  • 2021-01-04 06:08

    The correct way to check whether two objects are equal is to use ==. Therefore:

    array_uintersect($arr1, $arr2, function ($a1, $a2) { return $a1 == $a2; });
    
    0 讨论(0)
  • 2021-01-04 06:12

    The correct solution would be:

    array_uintersect($arr1, $arr2, function ($a1, $a2) { return $a1 != $a2; });
    

    Note the != in the callback function as opposed to the answer from @Artefacto. Based on the documentation of array_uintersect, the callback function has to return 0 (false) if array items are equal.

    0 讨论(0)
  • 2021-01-04 06:13

    You can use array_uintersect in conjunction with spl_object_hash, see an example:

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

    where '$a' and '$b' are arrays of some objects that you want to intersect.

    0 讨论(0)
提交回复
热议问题