micah1701 Posted June 15, 2006 Share Posted June 15, 2006 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 $winnersif 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. Quote Link to comment https://forums.phpfreaks.com/topic/12080-count-matching-results-in-two-arrays/ Share on other sites More sharing options...
zq29 Posted June 15, 2006 Share Posted June 15, 2006 Shorter solution:[code]<?php$matches = array_intersect($winners,$picks);echo "You got ".count($matches)." right";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12080-count-matching-results-in-two-arrays/#findComment-46076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.