PHP arrays, trying to match three numbers -
im trying match 3 or more numbers 2 seperate arrays, have far code matching first 3 numbers, when need compare 6 numbers , see if there 3 in common ? have tried comparing 6 numbers not work. appreciated. jessica
foreach($lottotickets $y => $yvalue) { if($i == 0) { echo " "; } else{ if((($winner[0] == $lottotickets[$y][0]) || ($winner[0] == $lottotickets[$y][1]) || ($winner[0] == $lottotickets[$y][2]) || ($winner[0] == $lottotickets[$y][3]) || ($winner[0] == $lottotickets[$y][4]) || ($winner[0] == $lottotickets[$y][5])) && (($winner[1] == $lottotickets[$y][0]) || ($winner[1] == $lottotickets[$y][1]) || ($winner[1] == $lottotickets[$y][2]) || ($winner[1] == $lottotickets[$y][3]) ||($winner[1] == $lottotickets[$y][4]) || ($winner[1] == $lottotickets[$y][5])) && (($winner[2] == $lottotickets[$y][0]) || ($winner[2] == $lottotickets[$y][1]) || ($winner[2] == $lottotickets[$y][2]) || ($winner[0] == $lottotickets[$y][3]) ||($winner[2] == $lottotickets[$y][4]) || ($winner[2] == $lottotickets[$y][5]))) echo "<b>three winning numbers id = </b>" .$y; }
you use array_diff() purpose. returns array containing differences within 2 given arrays.
if determine differences between user's lotto ticket , correct numbers, amount of wrong numbers.
subtracting integer 6* gives amount of correctly chosen numbers.
foreach ($lottotickets $y => $yvalue) { if($i == 0) { echo " "; } else { $diff = array_diff($lottotickets[$y], $winner); $correctnumbers = 6 - count($diff); if ($correctnumbers >= 3) { echo "<b>(at least) 3 winning numbers id = </b>" . $y; } } }
here minimal working sample: http://codepad.org/owrdv5xe
*) 6 not magic number here (→ lotto)
as why code not work (in cases): because compare first 3 correct numbers chosen ones. have compare numbers. involves having counter variable storing amount of correctly chosen numbers.
if want stick current solution, @ least use in_array() , loop. but solution provided @ top of answer better.
Comments
Post a Comment