【问题标题】:I need to loop through a list of arrays and return matches [closed]我需要遍历数组列表并返回匹配项[关闭]
【发布时间】:2014-02-12 18:30:09
【问题描述】:

我正在构建一个游戏来检查彩票,因此我正在尝试构建一个循环,该循环将通过 50 个彩票行的列表循环 6 个彩票号码。

我有一个包含 6 个不重复数字的数组。我想通过 50 个数组循环这个数组,每个数组有 6 个数字,但在每个数组中没有数字可以重复。

我想返回数组中的数字与其他 50 个数组中的任何数字匹配的次数。

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

我对 PHP 还很陌生,正在努力寻找最简单的方法。

我正在使用这个游戏来提高我的 PHP 知识,任何帮助将不胜感激。

【问题讨论】:

标签: php


【解决方案1】:

试试这样的:

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

?>

【讨论】:

    猜你喜欢
    • 2017-08-02
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    相关资源
    最近更新 更多