2014-02-12 91 views
0

我正在構建一個遊戲來檢查彩票,所以我正在嘗試構建一個循環,通過50個彩票線列表循環6個彩票號碼。我需要遍歷數組列表並返回匹配

我有6個非重複數字的數組。我想通過50個數組來循環這個數組,每個數組有6個數字,但是在每個數組中,沒有數字可以被複制。

我想返回數組中的數字與其他50個數組中的任何數字匹配的次數。

1 number = 20 matches 
2 numbers = 10 matches 
3 numbers = 1 match. 

我足夠新到PHP,並試圖找到最簡單的方法來做到這一點。

我使用這個遊戲來提高我對PHP的知識,任何幫助將不勝感激。

+0

http://stackoverflow.com/questions/13633954/how-do-i-count-occurrence-of-duplicate-items-in-array –

回答

0

嘗試這樣:

<?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"; 
} 

?>