Search code examples
phpforeachisset

How to find unique values in two different arrays using isset () and foreach


I want to find unique values in two different arrays. For example, I have $srr1 = [1,3,7,10,13] and $arr2 = [7,4,1,5,3]. I go through arrays

foreach ($arr1 as $key1 => $val1) {
    foreach ($arr2 as $key2 => $val2) {
       if (isset($arr1[$val2]) && $ arr1[$val2] != $val1) {
          echo "$arr1[$val2]"; // here I get 13 3 10 13 10 13 3 10 13 3 3 10, but 3 is superfluous here, because the number 3 is in both the first array and the second array.
       }
    }
}

Can you please tell me how to solve this problem. Thank you in advance.


Solution

  • There are several ways to do it; it just depends on how creative you want to be. This shows three approaches: my preferred method, one that meets you criteria, and lastly a rather not-DRY (don’t repeat yourself) procedural way that I don't recommend.

    The first two are nearly identical in the way they work; the first just uses a php built-in function to do it in C. There is not isset() required for the second example; you're just iterating through existing array keys.

    Working example at http://sandbox.onlinephpfunctions.com/code/1488860430866c1e5c9428818753cf70b0258f26

    <?php
    
    $arr1 = [1,3,7,10,13];
    $arr2 = [7,4,1,5,3];
    
    function getUnique(array $a1, array $a2) : array
    {
        // initialize output
        $unique = [];
        foreach($a1 as $v) {
            // ignore if present in other array
            if(in_array($v, $a2)) { continue; }
            $unique[] = $v;
        }
        return $unique;
    }
    
    print "Example 1\n";
    print_r(getUnique($arr1, $arr2));
    print_r(getUnique($arr2, $arr1));
    print "-----\n\n";
    
    
    function getUnique2(array $a1, array $a2) : array
    {
        // initialize output
        $unique = [];
        foreach($a1 as $v) {
    
            // for each iteration of outer array, initialize flag as not found
            $foundInArray = false;
            foreach($a2 as $v2) {
    
                // if you find a match, mark it as so.
                if($v == $v2) {
                    $foundInArray = true;
    
                    // (optional) no need to test the rest of the inner array
                    break;
                }
            }
    
            // only store those not marked as found
            if(!$foundInArray) {
                $unique[] = $v;
            }
        }
        return $unique;
    }
    
    print "Example 2\n";
    print_r(getUnique($arr1, $arr2));
    print_r(getUnique($arr2, $arr1));
    print "-----\n\n";
    
    
    
    // or, a more obscure way
    $flip = [];
    foreach($arr1 as $v) {
        // increment if we’ve seen this key before
        if(array_key_exists($v, $flip)) {
            $flip[$v]++;
        // initialize if we haven’t 
        } else {
            $flip[$v]=0;
        }
    }
    
    // repeat code in a very wet way…
    foreach($arr2 as $v) {
        if(array_key_exists($v, $flip)) {
            $flip[$v]++;
        } else {
            $flip[$v]=0;
        }
    }
    
    foreach($flip as $k => $v) {
        if($v==0) {
            print "$k\n";
        }
    }