checking in two arrays ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

checking in two arrays ??

Post 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.
jrja
Forum Newbie
Posts: 17
Joined: Wed Nov 19, 2008 10:28 am

Re: checking in two arrays ??

Post 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
ramya123
Forum Newbie
Posts: 13
Joined: Thu Sep 11, 2008 6:11 am

Re: checking in two arrays ??

Post 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
)
Post Reply