0

Can somebody explain, what is 1 and -1 in this code: ($a>$b)?1:-1; ?

I know the Array ( [c] => blue ) is returning because the key c is not exist in $a2 and key_compare_func need to return number smaller, equal or bigger then 0.

But I don't understand, how I get the Array ( [c] => blue ), when the key_compare_func returns 0, 1 and -1:

function myfunction($a,$b) {
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}

$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");

$result=array_diff_ukey($a1,$a2,"myfunction");
dWinder
  • 11,597
  • 3
  • 24
  • 39
  • Compare function require to have 3 possible option - `0` for equal, positive and negative. `1` and `-1` are example for positive and negative result – dWinder Jan 27 '19 at 08:23
  • Thanks for your comment. However, how it returns this result ? Array ( [c] => blue ) – Iynga Iyngaran Iyathurai Jan 27 '19 at 08:30
  • Possible duplicate of [Reference — What does this symbol mean in PHP?](https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – yivi Jan 28 '19 at 06:59
  • Updated my post with deeper explanation of how `array_diff_ukey` works – dWinder Jan 28 '19 at 07:54

3 Answers3

0

If ($a>$b) is true (right after the ?) - you return 1. else (right after the :) will return -1.

It's a short way of writing this:

if ($a>$b) {
    return 1; 
} else {
    return -1; 
}
Imnotapotato
  • 5,308
  • 13
  • 80
  • 147
0

As you can see in array-diff-ukey documentation the "key_compare_func" need to return number smaller, equal or bigger then 0. the numbers 1 and -1 are just example for this results.

In your case you can simply use strcmp as it return the same logic.

You have Array ( [c] => blue ) in the return because the key c is not exist in the $a2 array as it say:

Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

Edited

Specifically in array-diff-ukey you only need the return 0 because that the way this function is decided the keys are the same so in your example you can define it as:

function myfunction($a,$b) {
    if ($a === $b)
        return 0;
    return 1; // or -1 or 3 or -3 **just not 0** 
}

Consider that as the logic behind array-diff-ukey:

array function array-diff-ukey($a1, $a2, $compareFunction) {
    $arr = array(); // init empty array
    foreach($a1 as $key1 => $value1) { // for each key in a1
        $found = false;
        foreach($a1 as $key2 => $value2) { //search for all keys in a2
            if ($compareFunction($key1, $key2) == 0)
                $found = true;  // found a key with the same
        }
        if ($found === false) // add the element only if non is found
            $arr[$key1] = $value1; 
    }
    return $arr;
}
dWinder
  • 11,597
  • 3
  • 24
  • 39
  • Thanks for your comment. I know the Array ( [c] => blue ) in the return because the key c is not exist in the $a2 and "key_compare_func" need to return number smaller, equal or bigger then 0. the numbers 1 and -1. But I don't undferstand, how I get the Array ( [c] => blue ), when the key_compare_func returns 0, 1 and -1. – Iynga Iyngaran Iyathurai Jan 28 '19 at 07:32
  • @lyathurailyngaran when the function compare all of the key in both of your array she check for similar (for case where the compare function return 0) - for all cases except `c` there is a key in the `$a2` who the call `myfunction(@KEY_FROM_$A1@,@KEY_FROM_$A2@)` return 0 (same key) except for `c` - that why he the only one returns – dWinder Jan 28 '19 at 07:43
  • So this line return ($a>$b)?1:-1; must return anything other than 0 ? example return 10 also must give the same results ? – Iynga Iyngaran Iyathurai Jan 28 '19 at 12:09
  • If you using it with `array-diff-ukey` then yes. If this post help you consider marking it as accepted – dWinder Jan 28 '19 at 12:09
0

It is the ternary operator in PHP. You can say it as shorthand If/Else. Here is an example:

/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // if $var greater than 2 
                                                      // return true 
                                                      // else false

If it is being difficult for you to understand, you can change it with:

if ($a===$b)
      {
        return 0;
      }
else if($a > $b)
    {
      return 1;
    }
else
   {
     return -1;
   }
Saroj Shrestha
  • 2,696
  • 4
  • 21
  • 45