0

I have created the following script for creating an array_uintersect alogrithm.

function compare2D($topic, $nomination): int
{
    if (is_array($nomination)) {
        return in_array($topic, $nomination) ? 0 : 1;
    }
    else if (is_array($topic)) {
            return in_array($nomination, $topic) ? 0 : 1;
    }

    return strcmp($topic, $nomination);
}

$arr  = [['REMOTE', 'REMOTE_PREMIUM'], 'SECURE'];
$topic = ['SECURE', 'REMOTE'];


var_dump(array_uintersect($topic, $arr, 'compare2D'));

When I run it, the result is

array(1) {
  [0] =>
  string(6) "SECURE"
}

Where it should be returning

array(2) {
  [0] =>
  string(6) "SECURE"
  [1] =>
  string(6) "REMOTE"
}

I found that this is only dependent on the array in 2nd argument to _uintersect having a sub-array at index 0 (first item). When I move the sub-array to any position except for the 1st, i.e:

$arr  = ['SECURE', ['REMOTE', 'REMOTE_PREMIUM']];
// or
$arr  = ['SOMEVALUE', ['REMOTE', 'REMOTE_PREMIUM'], 'SECURE'];

...the intersection works fine, and I get the intended result above.

Does anyone know some algorithmic rules of php intersection that I am not aware of?

4
  • Just tried the code and get array(2) { [0] => string(4) "PLUM" [1] => string(9) "RASPBERRY" } Commented Sep 22, 2021 at 6:43
  • Oh wait so did I scratches head. I put obsfuscated strings on here - but the details are not that sensitive, so I will change them back momentarily Commented Sep 22, 2021 at 6:45
  • @NigelRen Reverted back to actual strings, can try again Commented Sep 22, 2021 at 7:06
  • It's down to what you return from return in_array($topic, $nomination) ? 0 : 1;, depends on what strings you put it it may or may not work. Unfortunately I haven't got time to investigate further. (see php.net/manual/en/function.array-uintersect.php#72841) Commented Sep 22, 2021 at 7:19

1 Answer 1

1

I think you can switch the logic of returning 1 and 0, so return 1 when the value is in the array as you want to find the intersection.

For the last part, using strcmp will return 0 if the strings are equal. In that case you can check if they are equal, and if they are, then return 1.

function compare2D($topic, $nomination): int
{
    if (is_array($nomination)) {
        return in_array($topic, $nomination) ? 1 : 0;
    }
    if (is_array($topic)) {
        return in_array($nomination, $topic) ? 1 : 0;
    }
    
    return strcmp($topic, $nomination) === 0 ? 1 : 0;
}

$topic = ['SECURE', 'REMOTE'];
$arr  = [['REMOTE', 'REMOTE_PREMIUM'], 'SECURE'];

$res = array_uintersect($topic, $arr, 'compare2D');
var_dump($res);

Output

array(2) {
  [0]=>
  string(6) "SECURE"
  [1]=>
  string(6) "REMOTE"
}

Php demo | Php demo with more nested arrays

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response - so you are saying that I should simply invert my responses and it works? As per your demos it looks like you got it working like that. It is weird because in the php uintersect docs php.net/manual/en/function.array-uintersect.php#72841 it shows that you can simply plug in strcmp and the default return will work to intersect 1D arrays. Anyways who am I to argue with your results!
@ScottAnderson I ran a few tests, and it seems to get the desired results. I also had another variant to only do the string compare if both parameters are not an array. It gives the expected results, but you could do some more testing to see if it does not miss anything. See 3v4l.org/qJeuI

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.