Do it like this:
<?php
$array1 = array('1','2','3','4');
$array2 = array('1','3','5','7');
//case 1:
print_r(array_intersect($array1, $array2));
//case 2:
print_r(array_diff($array2, $array1));
?>
This outputs the values of array (what you wanted earlier before question was changed):
Array
(
[0] => 1
[2] => 3
)
Array
(
[2] => 5
[3] => 7
)
And, if you want to use if-else
, do it like this:
<?php
$array1 = array('1','2','3','4');
$array2 = array('1','3','5','7');
$intesect = array_intersect($array1, $array2);
if(count($intesect))
{
echo 'the needle';
print_r($intesect);
}
else
{
echo'the haystack LESS the needle ';
print_r(array_diff($array2, $array1));
}
?>
This outputs:
the needle
Array
(
[0] => 1
[2] => 3
)
shamittomar
2010-09-04 07:35:02