hi
i want to know how to array_intersect for object array.
thanks and advance
hi
i want to know how to array_intersect for object array.
thanks and advance
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:
Then you can't use it on array of objects if your objects not implements unique conversion to string.
The correct way to check whether two objects are equal is to use ==
. Therefore:
array_uintersect($arr1, $arr2, function ($a1, $a2) { return $a1 == $a2; });
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