Search code examples
phparrayssortingarray-difference

PHP Multi-Array Difference


I have arrays containing objects that look like the following, first array:

Array
(
[1] => stdClass Object
    (
        [matchID] => 1
        [tm] => 2014-01-16 08:55:13
        [playertm] => 2014-01-16 08:55:14
    )

[2] => stdClass Object
    (
        [matchID] => 2
        [tm] => 2014-01-16 09:53:50
        [playertm] => 2014-01-16 09:53:52
    )

[3] => stdClass Object
    (
        [matchID] => 3
        [tm] => 2014-01-16 09:58:49
        [playertm] => 2014-01-16 09:58:57
    )

[4] => stdClass Object
    (
        [matchID] => 4
        [tm] => 2014-01-17 08:44:34
        [playertm] => 2014-01-17 08:44:35
    )
)

Second array:

Array
(
[3] => stdClass Object
    (
        [matchID] => 3
        [tm] => 2014-01-16 09:58:49
        [playertm] => 2014-01-16 09:58:57
    )

[4] => stdClass Object
    (
        [matchID] => 4
        [tm] => 2014-01-17 08:44:34
        [playertm] => 2014-01-17 08:44:38
    )

[5] => stdClass Object
    (
        [matchID] => 5
        [tm] => 2014-01-19 08:44:34
        [playertm] => 2014-01-19 08:44:38
    )
)

And am trying to synchronize each array based on the times. I want 4 results to be returned:

  1. The objects in the first array that have a time more recent than the second array
  2. The objects in the second array that have a time more recent than the first array
  3. The objects in the first array that have a 'playertm' more recent than the second array
  4. The objects in the second array that have a 'playertm' more recent than the first array

Some results may not be in each array and will need to be returned, however the array keys will always match.

I am using the 'array_udiff' function and so far have the following:

function tmCompare($a, $b)
{
    return strtotime($a->tm) - strtotime($b->tm);
}
function ptmCompare($a, $b)
{
    return strtotime($a->playertm) - strtotime($b->playertm);
}

$df1 = array_udiff($a, $b, 'tmCompare');
$df2 = array_udiff($b, $a, 'tmCompare');

$df3 = array_udiff($a, $b, 'ptmCompare');
$df4 = array_udiff($b, $a, 'ptmCompare');

Which appears to return the differences, however array [4] is returned in each of the last 2 functions, whereas I only want it to be returned if the time is larger rather than only different.

I have tried

return (strtotime($a->playertm) > strtotime($b->playertm)) ? -1 : 0;

and similar but cannot seem to get the correct results. Am I missing something simple here or going about this wrong?

Edit: here is a quick pastebin to get the code running http://pastebin.com/gRz9v2kz

Thanks for any help.


Solution

  • I'm not sure why this happens, but the use of array_udiff() seems counterintuitive. I've rewritten the requirements in two functions to perform the comparison and iterate the array:

    function getCompareFunction($field)
    {
            return function($a, $b) use ($field) {
                    return strcmp($a->{$field}, $b->{$field});
            };
    }
    
    function getBigger($a, $b, $compare)
    {
            $res = array();
    
            foreach ($a as $k => $v) {
                    if (!isset($b[$k]) || $compare($v, $b[$k]) > 0) {
                            $res[$k] = $v;
                    }
            }
    
            return $res;
    }
    
    $biggerTime = getCompareFunction('tm');
    $biggerPlayerTime = getCompareFunction('playertm');
    
    print_r(getBigger($a, $b, $biggerTime)); // [1, 2]
    print_r(getBigger($b, $a, $biggerTime)); // [4, 5]
    print_r(getBigger($a, $b, $biggerPlayerTime)); // [1, 2]
    print_r(getBigger($b, $a, $biggerPlayerTime)); // [4, 5]