array intersect for object array php

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

I want to know how to array_intersect for object array.

相关标签:
8条回答
  • 2021-01-04 06:15

    array_intersect() returns an array containing all the values of array1 that are present in all the arguments.

    Then what mean present in this context (exacly this function), i found on php.net my answer:

    Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

    Then you can't use it on array of objects if your objects not implements unique conversion to string.

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

    nice toString function is already implemented and is called serialize ;) so

    array_map(
        'unserialize',
        array_intersect(
            array_map(
                'serialize',
                $obj1
            ), 
            array_map(
                'serialize', 
                $obj2
            )
        )
    );
    

    will do the work, example mentioned higher don't work 'cause array_intersect work's only with strings as someone mentioned too

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