3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $obj1 = new stdClass(); $obj1->attr = 'Rod'; $obj2 = new stdClass(); $obj2->attr = 'Jane'; $obj3 = new stdClass(); $obj3->attr = 'Freddy'; $array1 = array( $obj1, $obj2, $obj3 ); $array2 = array( $obj1, $obj3, $obj2 ); function return1($a,$b) { if ($a->attr == $b->attr) { return 0; } else { return 1; } } function returnNeg1($a,$b) { if ($a->attr == $b->attr) { return 0; } else { return -1; } } //$diff1 = array_diff($array1, $array2); $diff2 = array_udiff($array1, $array2, 'returnNeg1'); $diff3 = array_udiff($array1, $array2, 'return1'); //$int = array_intersect($array1, $array2); $int2 = array_uintersect($array1, $array2, 'returnNeg1'); $int3 = array_uintersect($array1, $array2, 'return1'); //var_dump($diff1); var_dump($diff2); var_dump($diff3); //var_dump($int1); var_dump($int2); var_dump($int3);
based on FFAer
Output for 7.0.0 - 7.2.0
array(1) { [1]=> object(stdClass)#2 (1) { ["attr"]=> string(4) "Jane" } } array(2) { [0]=> object(stdClass)#1 (1) { ["attr"]=> string(3) "Rod" } [2]=> object(stdClass)#3 (1) { ["attr"]=> string(6) "Freddy" } } array(2) { [0]=> object(stdClass)#1 (1) { ["attr"]=> string(3) "Rod" } [2]=> object(stdClass)#3 (1) { ["attr"]=> string(6) "Freddy" } } array(1) { [1]=> object(stdClass)#2 (1) { ["attr"]=> string(4) "Jane" } }
Output for 5.3.19 - 5.6.28
array(2) { [0]=> object(stdClass)#1 (1) { ["attr"]=> string(3) "Rod" } [2]=> object(stdClass)#3 (1) { ["attr"]=> string(6) "Freddy" } } array(1) { [2]=> object(stdClass)#3 (1) { ["attr"]=> string(6) "Freddy" } } array(1) { [1]=> object(stdClass)#2 (1) { ["attr"]=> string(4) "Jane" } } array(2) { [0]=> object(stdClass)#1 (1) { ["attr"]=> string(3) "Rod" } [1]=> object(stdClass)#2 (1) { ["attr"]=> string(4) "Jane" } }