0

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.

2
  • Have you tried to debug your compare_followers function ? Try to output the $f1, $f2, $s1, $s2 and the output of strcmp($s1, $s2) and make sure that it returns a value that make sense to array_udiff Commented Nov 14, 2014 at 18:16
  • Hi Nico, Yes I've done this. I can compare elements of the array and also individual follower objects, and the output is as it should be. Also, to simplify things, I also tried to make compare_followers always return 1 or always return 0, but the output of array_udiff was always still nil with my code above. Commented Nov 14, 2014 at 18:33

1 Answer 1

0

Try this. Careful of what the arrays actually contain.

<?php

class follower {
    public $friend;
    public $newposts;

    public function __construct($friend, $newpost) {
        $this->friend = $friend;
        $this->newposts = $newpost;
    }
}

$object_array1 = array();
$holder = new follower('me', 'first');
array_push($object_array1, $holder);

$object_array2 = array();
$holder = new follower('you', 'second');
array_push($object_array2, $holder);

function compare_followers($f1, $f2) {
    $s1 = $f1->friend;
    $s2 = $f2->friend;
    return strcmp($s1, $s2);
}

$object_array = array_udiff($object_array2, $object_array1, 'compare_followers');

var_dump($object_array);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! So it's not null anymore, but the format still isn't quite the same as the original arrays when json encoded. Here's the result of the array push - an array of objects/dictionaries:[{"friend":"brooklynsage","newposts":null},{"friend":"jackR","newposts":null},...etc whereas after the array_udiff with your syntax, I still have a dictionary of dictionaries/objects:{"12":{"friend":"jonjon","newposts":null},"14":{"friend":"laylaohio2","newposts":null}}...
json_encode returns array [] only when keys are int and in order. That means that array(0=>'val',1=>'val') will give array. but array(0=>'val',2=>'val') won't. Assoc array won't give array neither

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.