我正在建立一个游戏,以检查彩票,所以我试图建立一个循环,将循环的6个彩票号码,通过一个名单50个彩票线。
我有一个有6个非重复数字的数组。我想通过50个数组循环这个数组,每个数组都有6个数字,但是在每个数组中,没有一个数字可以重复。
我想返回数组中的数字与其他50个数组中的任意一个数字匹配的次数。
1 number = 20 matches
2 numbers = 10 matches
3 numbers = 1 match.我对PHP有足够的了解,并试图找到最简单的方法来做到这一点。
我正在使用这个游戏来提高我的PHP知识,任何帮助都将不胜感激。
发布于 2014-02-12 10:30:09
试着做这样的事情:
<?php
//array to store the number of matches
$matchesArr = array(0,0,0,0,0,0,0);
//your lottery numbers
$myNumbers = array(1,2,3,4,5,6);
//the past lottery results
$pastResults = array(
array(10,12,1,2,34,11),
array(10,12,1,2,34,11),
array(10,12,1,2,34,11)
);
//loop through each past lottery result
foreach($pastResult as $pastResult){
$matches = 0;
//do any of your numbers appear in this result?
foreach($myNumbers as $myNumber){
if(in_array($myNumber, $pastResult)){
$matches++;
}
}
//add the number of matches to the array
$matchesArr[$matches]++;
}
//print the number of matches
foreach($matchesArr as $index=>$matches){
echo $index." number = ".$matches."\n";
}
?>https://stackoverflow.com/questions/21725026
复制相似问题