Find the difference in an array using a loop

I am new to PHP. I want to find the difference in an array without using any array function like array_diff()or in_array().

This is my code.

$a = array('a','b','c','d','k');
$b = array('g','h','i','b','a','d','c');
$match = array();
$miss_match = array();

$count_a = count($a);
$count_b = count($b);

for($i=0; $i<$count_a;$i++)
    {
        for($j=0; $j<$count_b;$j++)
            {
            if($a[$i]==$b[$j])
                {
                $match[] = $a[$i];
                break;
                }
            else
                {
                $miss_match[] = $b[$j];
                }   
            }
    }
print_r($match).'<br />';
print_r($miss_match);

And I get this result

Array ( [0] => a [1] => b [2] => c [3] => d ) 
Array ( [0] => g [1] => h [2] => i [3] => b [4] => g [5] => h 
[6] => i [7] => g [8] => h [9] => i [10] => b [11] => a [12] => d 
[13] => g [14] => h [15] => i [16] => b [17] => a [18] => g 
[19] => h [20] => i [21] => b [22] => a [23] => d [24] => c ) 

Expected Result

Array ([0] => g [1] => h [2] => i [3] => k)

Please suggest the best solution. thanks

+4
source share
2 answers

You add an element to the array miss_matchfor each element of another array that does not match. But if it does not match this element, it may still match the later element.

You need to go through the entire array before determining that it does not match any of them.

for($i=0; $i<$count_a;$i++)
{
    for($j=0; $j<$count_b;$j++)
    {
        $matched = false;
        if($a[$i]==$b[$j])
        {
            $match[] = $a[$i];
            $matched = true;
            break;
        }
    }
    if (!$matched)
    {
        $miss_match[] = $a[$i];
    }   
}

Result:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
Array
(
    [0] => k
)
+1
source

Here is my new code for finding the difference of arrays using a loop

$b = array('a','b','c','d','x','b');
$a = array('g','h','i','b','a','d','c');
$mismatch = array();
$count_a = count($a);
$count_b = count($b);
$match = array();
for( $i = 0; $i<count($a); $i++ )
{
        $found_a ='no';
        $foundss ='no';
        for($j=0; $j<count($b); $j++ )
        {
             if($a[$i] == $b[$j])
             {
                 $found_a ='yes';
                 $match[]=$a[$i];
            }            

        }
        if($found_a =='no')
        {
             for($t=0;$t<count($mismatch);$t++)
            {
                if($a[$i]== $mismatch[$t])
                   {
                       $foundss ='yes';
                       break;

                    }
            }
                if($foundss == 'no')
                {  
                    $mismatch[]=$a[$i];
                } 

        }

}

for($z=0; $z< count($b); $z++)
{ $found_b ='no';
$found_match ='no';
   for($t=0;$t<count($mismatch);$t++)
    {
        if($b[$z]==$mismatch[$t])
        {
             $found_b ='yes';
        }    
    }
    if($found_b =='no')
    {
        for($d =0; $d<count($match);$d++)
                   {
                     if($b[$z] ==$match[$d] )  
                     {
                         $found_match='yes';
                         $break;
                     } 
                   }
                if($found_match=='no')   
        $mismatch[]=$b[$z];
    }
}
print_r($mismatch);
0
source

Source: https://habr.com/ru/post/1525051/


All Articles