array intersect for object array php

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

I want to know how to array_intersect for object array.

8条回答
  •  时光说笑
    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.

提交回复
热议问题