...

View Full Version : Compare and Match Arrays



ptmuldoon
03-02-2007, 09:24 PM
I was introduced and learned about comparing arrays about a week ago, using the array_diff function.

But is there a way to compare 2 arrays and have it return only the matches?

Would you use the in_array function to do that? Would that return all matches? Or just a True or False?

Inigoesdr
03-02-2007, 09:33 PM
http://www.php.net/manual/en/function.array-intersect.php

ptmuldoon
03-02-2007, 10:00 PM
I'm not sure if thats what I'm looking for, but I amm still too green to truly understand all of the array functions out there.

Basically, I'm looking to search two arrays, and foreach match that exists, I want to be able to echo out that number.

I had though using in_array was the correct approach. I would think the below code should, but its not finding a match.


// Set the Variables
$capitals = '11,12';
$player_states = '11,12';

//Explode them into arrays
$capitals = explode(',',$capitals);
$player_states = explode(',',$player_states);

if(in_array($capitals, $player_states)) {
echo 'We have a match';
}

Inigoesdr
03-02-2007, 10:10 PM
I'm not sure if thats what I'm looking for essentially if I have, but I'm still to green to truly understand all of the array functions out there.

Basically, I'm looking to search two arrays, and foreach match that exists, I want to be able to echo out that number.

I had though using in_array was the correct approach.



foreach($capitals as $k)
{
if(in_array($k, $player_states))
echo $k;
}
I think I misread that. I think the above code is what you're looking for.

aedrin
03-02-2007, 10:45 PM
but I amm still too green to truly understand all of the array functions out there.

Sit down, create a new PHP file. And start trying them out.

There's quite some useful ones out there.

Know your tools.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum