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
source
share