Page 1 of 1

checking in two arrays ??

Posted: Wed Nov 19, 2008 10:35 pm
by PHPycho
Hello forums!!!
Case:
Suppose we have two arrays(returned by queries)
$array1 and $array2 of different sizes for example (two cases here)
a>

Code: Select all

$array1 = array(1,2,3);
$array2 = array(1,2);
b>

Code: Select all

$array1 = array(1);
$array2 = array(1,2,4,5);
I would like to check presence of any values of $array1 in $array2.
How to accomplish this, any ideas.
Thanks in advance for the suggestions.

Re: checking in two arrays ??

Posted: Wed Nov 19, 2008 11:15 pm
by jrja
Use

Code: Select all

$array1 = (1, 2, 3);
$array2 = (4, 5, 6);
$result = array_merge($array1, $array2);
print_r($result);
http://www.php.net/array_merge

Re: checking in two arrays ??

Posted: Thu Nov 20, 2008 1:20 am
by ramya123
This code might help you

Code: Select all

<?php
   $array1 = array(1, 2, 3);
   $array2 = array(1, 2);
   $result = array_intersect($array1, $array2);
print_r($result);
?>
 
The above code returns an array containing all of the values in array1 whose values exist in array2.i.e.,

Code: Select all

Array
(
    [0] => 1
    [1] => 2
)