Jump to content

count matching results in two arrays


micah1701

Recommended Posts

So I have two arrays.
the first lists the winners of a series of games:

$winners = Array
(
[0] => New England
[1] => Buffalo
[2] => pittsburgh
[3] => St. Louis
)

and the other is a person's guess of who will win

$picks = Array
(
[0] => New England
[1] => NY Giants
[2] => Dallas
[3] => St. Louis
)

What is the best way to compare the arrays to see how many guesses in $picks match the results in $winners

if I had 1 pick, I could use in_array but I need to check each value in picks against the $winners array and determine a value for total matches. (ie, "2" in the example above)

thoughts on the best way to do it?
I always come up w/ hack jobs and make it 20 times more complicated then it needs to be.

[EDIT]

ok, well here's what I have:

[code]
  foreach($picks as $pick){
  if(in_array($pick,$winners)){
   $total = $total + 1;
   }
  }
  echo "you got $total correct";
[/code]

I guess thats pretty simple if no one has a better idea.
Link to comment
https://forums.phpfreaks.com/topic/12080-count-matching-results-in-two-arrays/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.