tags:

views:

125

answers:

3

hi

i want to know how to array_intersect for object array.

thanks and advance

A: 

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.

Svisstack
That's innacurate. There are at least three ways objects can be converted into a string. They may be a PHP class and implement __toString, they may have a cast handler that accepts IS_STRING and they may have a get handler that returns a zval that is convertable to a string.
Artefacto
+1 @Artefacto, submit an answer with an example using arbitrary objects and array_intersect().
Dolph
If your objects not implements unique conversion to string.
Svisstack
A: 

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

array_uintersect($arr1, $arr2, function ($a1, $a2) { return $a1 == $a2; });
Artefacto
this does not works..u have any other coding?
zahir hussain
You'll have to be more specific. What doesn't work? Are there any error messages? What error messages? The ouput is not the expected? What was expected? What did you get?
Artefacto
A: 

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

Ian Draco