2

hi

i want to know how to array_intersect for object array.

thanks and advance

flag

61% accept rate
What do you mean by "object array" - do you mean an array of objects? – thetaiko 2 days ago
yes, that is array of objects. – zahir hussain 2 days ago

2 Answers

0

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.

link|flag
1  
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 2 days ago
+1 @Artefacto, submit an answer with an example using arbitrary objects and array_intersect(). – Dolph Mathews 2 days ago
If your objects not implements unique conversion to string. – Svisstack 2 days ago
0

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

array_uintersect($arr1, $arr2, function ($a1, $a2) { return $a1 == $a2; });
link|flag
this does not works.. u have any other coding? – zahir hussain 2 days ago
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 yesterday

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.