Friday, October 14, 2011

How to find the common values of two array, without using array_intersect


$array1 = array(’3′,’8′,’7′,’2′);
$array2 = array(’4′,’5′,’7′,’2′);
$length1=count($array1);
$length2=count($array2);
$common=array();
if ($length1 > $length2)
{
for ($i=0; $i<$length1; $i++)
{
if (in_array($array1[$i], $array2))
{
$common[]=$array1[$i];
}
}
}
else
{
for($i=0; $i<$length2; $i++)
{
if (in_array($array2[$i], $array1))
{
$common[]=$array2[$i];
}
}
}
print ‘
’;

print_r($common);
?>

No comments:

Post a Comment