Page 1 of 2

comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 7:47 am
by EODC
Hi, I am completely new to PHP and need some help!

I have two arrays ($one & $two) and I need to run an IF statment if a number from array $one is found in array $two

I just cant get to seem to get it to work.

Ive declared the variable and done the following code, however its just returning NO :( Can anyone help me?

Code: Select all

if (in_array( $one , $two))
{
Print ("YES");
}
else
{
Print ("NO");
}

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 8:25 am
by VladSun

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 8:27 am
by EODC
ok, sounds right, however how do i get my variable to be like that?

Do i need to explode it? or leave it as it is?

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 8:29 am
by VladSun
What?!? Please, elaborate ...

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 8:32 am
by EODC
Sorry - you will have to bear with me - I am completely new to this.

My current code is this.

Code: Select all

 
$group = $settings['one'];                            // (pulling settings!)
         $don = $settings['two'];         //(pulling settings!)
         $groups = explode(' ',$group); // Contains 4,5,2,7,6,3,9,10,1,8
         $dont = explode(' ',$don);  // Contains 4,6
         $diff = array_diff($groups,$dont);
         print_r($diff); // This returns "Array ( [0] => 4,5,2,7,6,3,9,10,1,8 ) "
 
Is there anyway to get it so that it takes away (-'s) the 4,6 from the other list??? That is what I need :)

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 8:47 am
by VladSun
$diff should already be the one with the excluded 4,6 values ...
Check what's in $groups, $dont variables...

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 8:53 am
by EODC
this

Code: Select all

 
print_r($groups);
print_r($dont);
 
returned this

Code: Select all

 
Array ( [0] => 4,5,2,7,6,3,9,10,1,8 ) Array ( [0] => 4,6 )
 

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 9:01 am
by VladSun
So, your explode() is somehow bad ;) Probably you want to explode by "," ?

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 9:04 am
by EODC
wicked - did it and it works fine!

OK so now, how can I put this into my If stament, so...

IF these numbers (from a variable) are in this variable then...

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 9:07 am
by VladSun
What have you tried so far ;) ?

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 9:12 am
by EODC
I havn't as of yet as I cant quite work out how to make my statement.

I will word it out, and perhaps you could assist?

If (numbers from this varaible) are in (numbers from this varaible) then
{
do some work!
}
else
{
do some other work!
}

I have currently only done in_array - which i believe checks if one value is in an array, i need to check for possible multiple values.

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 9:17 am
by VladSun
Why does it seem to me that you are repeating the question you've started with???
Try using the solution for it ...

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 9:22 am
by timWebUK
Loop through all the values of one array (using a for loop), comparing the value each time against the second array using in_array(), until there are no more values to check.

Is that what you're trying to do?

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 11:08 am
by AbraCadaver
If you are just checking that any of the values in $one are in $two, then:

Code: Select all

if(array_intersect($one, $two))
If you need to make sure all of the values from $one are in $two, then this is one way:

Code: Select all

if(array_intersect($one, $two) == $one)

Re: comparing two arrays - prob a simple answer

Posted: Tue Mar 02, 2010 11:43 am
by Weirdan
array_intersect more likely:

Code: Select all

 
$a = array(2, 3);
$b = array(1, 2, 3, 4);
if (array_intersect($a, $b)) {
   echo 'yup';
} else {
   echo 'nope';
}