I am pushing class objects onto two different arrays an then want to take the different of these arrays. However, when I use array push with array_udiff I cannot get array_udiff to output anything other than null. Here's the barebones of the code.
Class definition:
class follower
{
public $friend;
public $newposts;
}
Data assignment: for loop containing:
$holder = new follower();
....put data in $holder....
array_push($object_array1, $holder);
second for loop doing the same thing...
$holder = new follower();
....put data in $holder....
array_push($object_array2, $holder);
Function to compare objects in the object arrays:
function compare_followers($f1, $f2)
{
$s1 = $f1->friend;
$s2 = $f2->friend;
return strcmp($s1, $s2);
}
And finally unsuccessful attempt to compare these two arrays (the arrays look as they should, arrays of follower objects)
$object_array = array_udiff($object_array2, $object_array1, 'compare_followers');
$object_array is null. In addition to the code above, I tried playing around with the comparison function - making it a static function within the class or declaring it as an unnamed function in the array_udiff call, but neither of those things worked.
If I apply compare_followers to individual class members, that works fine. Similarly, if I push the following object onto my $object_array1 and $object_array2 the array_udiff works fine:
$holderA = array(
'friend' =>$holder->friend,
'newposts' => $holder->newposts
);
array_push($object_array2, $holder); (and same for $object_array1)
$object_array = array_udiff($object_array2, $object_array1, 'compare_followers');
The problem with this latter, working apprpoach is that it gives a dictionary of objects rather than an array of objects - I need an array.
Here are some of the stackoverflow questions that are relevant but don't address this: array_udiff not working as expected, How does array_udiff() work?, array intersect for object array php. Here's the documentation for this class (http://php.net/manual/en/function.array-udiff.php
What am I doing wrong?
Thanks for any suggestions.
Try this. Careful of what the arrays actually contain.