Jump to content

Archived

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

micah1701

count matching results in two arrays

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.

Share this post


Link to post
Share on other sites
Shorter solution:
[code]<?php
$matches = array_intersect($winners,$picks);
echo "You got ".count($matches)." right";
?>[/code]

Share this post


Link to post
Share on other sites

×

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.