3

I'm trying to compare two array with array_udiff, but it's very weired. it seems array_udiff not get the right answer. Here is the live demo. The result should be an empty array, but leave one element unfiltered out.

<?php

$string = '{
  "sakiniai": [

      {
        "Faktas": "A",
        "value": "true"
      },
      {
        "Faktas": "B",
        "value": "true"
      },
      {
        "Faktas": "A",
        "value": "false"
      }
  ]
}';

$sakiniais = json_decode($string, true)['sakiniai'];


$v = $sakiniais[0];
$arr[] = $v;
$v['value'] = $v['value'] == "true" ? "false" : "true";
$arr[] = $v;
var_dump($arr);
var_dump($sakiniais);


print_r(array_udiff($arr, $sakiniais, function($a, $b){
/*
var_dump($a);
var_dump($b);
var_dump($a == $b);
echo "\n\n\n";
*/
return $a == $b ? 0 : -1;}
));

the output

    array(2) {
  [0]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
  }
  [1]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(5) "false"
  }
}
array(3) {
  [0]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
  }
  [1]=>
  array(2) {
    ["Faktas"]=>
    string(1) "B"
    ["value"]=>
    string(4) "true"
  }
  [2]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(5) "false"
  }
}
Array
(
    [1] => Array
        (
            [Faktas] => A
            [value] => false
        )

)
LF00
  • 27,015
  • 29
  • 156
  • 295
  • Maybe you should describe what you are trying to achieve. I cannot find the original question that had this requirement – RiggsFolly May 16 '17 at 10:48
  • @RiggsFolly here I compare two array, all the elements of first array are in the second array, so the expected output should be an empty array. But the result array has one element. – LF00 May 16 '17 at 10:50

2 Answers2

10

The problem is that array_udiff is not performing the comparison between all values, and this seems to be caused by your compare function.

array_udiff() expects that the callable function is a real compare function, but you are returning always 0 and -1, but never 1.

Before doing its job, array_udiff() tries to order both arrays and remove duplicates too. If it can't rely on your comparison function, it can't perform all the needed comparison and some values are "jumped".

Look at all comments in the documentation expecially napcoder comment

Note that the compare function is used also internally, to order the arrays and choose which element compare against in the next round.

If your compare function is not really comparing (ie. returns 0 if elements are equals, 1 otherwise), you will receive an unexpected result.

This is demonstrated looking at your arrays

$arr

array(2) {
  [0]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
  }
  [1]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(5) "false"
  }
}

and $sakiniais

array(3) {
  [0]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
  }
  [1]=>
  array(2) {
    ["Faktas"]=>
    string(1) "B"
    ["value"]=>
    string(4) "true"
  }
  [2]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(5) "false"
  }
}

You should expect all the combinations between the two arrays to be tested, but the tested combinations (below) doesn't include A-False vs. A-False

array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
}
array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(5) "false"
}
bool(false)



array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
}
array(2) {
    ["Faktas"]=>
    string(1) "B"
    ["value"]=>
    string(4) "true"
}
bool(false)



array(2) {
    ["Faktas"]=>
    string(1) "B"
    ["value"]=>
    string(4) "true"
}
array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(5) "false"
}
bool(false)



array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
}
array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
}
bool(true)



array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
}
array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(5) "false"
}
bool(false)



array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(5) "false"
}
array(2) {
    ["Faktas"]=>
    string(1) "B"
    ["value"]=>
    string(4) "true"
}
bool(false)

In the correct answer at this post there are some other useful insights on how array_udiff works

You can change your return statement like this

if ($a < $b) {
    return -1;
} elseif ($a > $b) {
    return 1;
} else {
    return 0;
}

(if you are wondering how two arrays can be compared for less/greater, have a look at this link, in the section "Comparison with Various Types" and the "Example #2 Transcription of standard array comparison" example)

Community
  • 1
  • 1
LombaX
  • 17,265
  • 5
  • 52
  • 77
0

Nothing strange, everything works as expected. Take a look at arrays comparing:

$arraysAreEqual = ($a == $b); // TRUE if $a and $b have the same key/value pairs.
$arraysAreEqual = ($a === $b); // TRUE if $a and $b have the same key/value pairs in the same order and of the same types.

so, $arr[0] == $sakiniai[0] is true, but $arr[1] == $sakiniai[2] is false and the output is $arr[1]

$arr

array(2) {
  [0]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
  }
  [1]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(5) "false"
  }
}

$sakiniai

array(3) {
  [0]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(4) "true"
  }
  [1]=>
  array(2) {
    ["Faktas"]=>
    string(1) "B"
    ["value"]=>
    string(4) "true"
  }
  [2]=>
  array(2) {
    ["Faktas"]=>
    string(1) "A"
    ["value"]=>
    string(5) "false"
  }
}

result

Array
(
    [1] => Array
        (
            [Faktas] => A
            [value] => false
        )

)
rokas
  • 1,521
  • 9
  • 16
  • $arr[1] == $sakiniai[2] is false? why? – LF00 May 16 '17 at 10:54
  • because keys are not the same 1 != 2 – rokas May 16 '17 at 10:54
  • keys are not 1, 2, but Faktas and value – LF00 May 16 '17 at 10:55
  • no, you are wrong. `array_udiff` is used to compute the diference between single dimension arrays, so if you're giving multiarrays as parameters, the function will take keys from first dimension. If you want to compare multiarrays, you should write your own function – rokas May 16 '17 at 11:01
  • have you seen my compare funciton in the array_udiff? And here is the [manual](http://php.net/manual/zh/function.array-udiff.php) – LF00 May 16 '17 at 11:04