确定2组数字之间的匹配数,PHP

时间:2014-09-13 20:50:52

标签: php

感谢您提供的任何帮助:)

所以我有两组数字:

$num1win , $num2win, $num3win, $num4win, $num5win, $num6win

$num1, $num2, $num3, $num4, $num5, $num6

我正在使用以下代码来尝试匹配它们:

$match=0;
$match += match($num1win, $num1, $num2, $num3, $num4, $num5, $num6);
$match += match($num2win, $num1, $num2, $num3, $num4, $num5, $num6);
$match += match($num3win, $num1, $num2, $num3, $num4, $num5, $num6);
$match += match($num4win, $num1, $num2, $num3, $num4, $num5, $num6);
$match += match($num5win, $num1, $num2, $num3, $num4, $num5, $num6);
$match += match($num6win, $num1, $num2, $num3, $num4, $num5, $num6);

function match($win, $num1, $num2, $num3, $num4, $num5, $num6){
    $my_array = array_flip(array($num1, $num2, $num3, $num4, $num5, $num6));
    if (isset($my_array[$win])){
        return 1;
    }
}

这对我来说很好但是我的结果很奇怪,如果匹配的数字是$ num1win和($ num1,或$ num2)或$ num2win和($ num1,或者数字,结果给我一个匹配(只有1) $ num2)但没有别的

有人可以让我知道一个错误,或者他们是否有更清洁的方式完成工作

谢谢,

1 个答案:

答案 0 :(得分:0)

这是另一种可能的解决方案,它将数字与中奖号码进行比较并返回匹配总数:

<?php
// Make one array out of all the winning numbers:
$winners = array($num1win, $num2win, $num3win, $num4win, $num5win, $num6win);
// Make a second array out of all the other numbers:
$numbers = array($num1, $num2, $num3, $num4, $num5, $num6);
// Display the results:
print match_count($winners, $numbers);

/**
 * @return
 *  Total number of matches.
 */
function match_count($winners, $numbers) {
  return count(array_intersect($numbers, $winners));
}