comparing two arrays - prob a simple answer

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

EODC
Forum Newbie
Posts: 9
Joined: Fri Feb 26, 2010 4:06 am

comparing two arrays - prob a simple answer

Post 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");
}
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: comparing two arrays - prob a simple answer

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
EODC
Forum Newbie
Posts: 9
Joined: Fri Feb 26, 2010 4:06 am

Re: comparing two arrays - prob a simple answer

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: comparing two arrays - prob a simple answer

Post by VladSun »

What?!? Please, elaborate ...
There are 10 types of people in this world, those who understand binary and those who don't
EODC
Forum Newbie
Posts: 9
Joined: Fri Feb 26, 2010 4:06 am

Re: comparing two arrays - prob a simple answer

Post 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 :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: comparing two arrays - prob a simple answer

Post by VladSun »

$diff should already be the one with the excluded 4,6 values ...
Check what's in $groups, $dont variables...
There are 10 types of people in this world, those who understand binary and those who don't
EODC
Forum Newbie
Posts: 9
Joined: Fri Feb 26, 2010 4:06 am

Re: comparing two arrays - prob a simple answer

Post 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 )
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: comparing two arrays - prob a simple answer

Post by VladSun »

So, your explode() is somehow bad ;) Probably you want to explode by "," ?
There are 10 types of people in this world, those who understand binary and those who don't
EODC
Forum Newbie
Posts: 9
Joined: Fri Feb 26, 2010 4:06 am

Re: comparing two arrays - prob a simple answer

Post 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...
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: comparing two arrays - prob a simple answer

Post by VladSun »

What have you tried so far ;) ?
There are 10 types of people in this world, those who understand binary and those who don't
EODC
Forum Newbie
Posts: 9
Joined: Fri Feb 26, 2010 4:06 am

Re: comparing two arrays - prob a simple answer

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: comparing two arrays - prob a simple answer

Post by VladSun »

Why does it seem to me that you are repeating the question you've started with???
Try using the solution for it ...
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: comparing two arrays - prob a simple answer

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: comparing two arrays - prob a simple answer

Post 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)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: comparing two arrays - prob a simple answer

Post 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';
}
 
Post Reply