PHP:如何获得与大多数子数组匹配的组合?

PHP:如何获得与大多数子数组匹配的组合?,php,arrays,statistics,combinations,Php,Arrays,Statistics,Combinations,我有一个$myArray,子数组总是包含5个数字-数字按大小排序,不能在子数组中重复,但是$myArray中可以有更多“相同”的子数组(具有相同数字的子数组) $myArray = array( array(1,2,3,4,5), array(5,6,10,18,20), array(1,2,3,4,5), array(2,3,4,5,9), array(1,2,3,7,9), array(1,3,4,5,7), array(2,3,4,7,9), array(2,4,5,10,29), arr

我有一个
$myArray
,子数组总是包含5个数字-数字按大小排序,不能在子数组中重复,但是
$myArray
中可以有更多“相同”的子数组(具有相同数字的子数组)

$myArray = array(
array(1,2,3,4,5),
array(5,6,10,18,20),
array(1,2,3,4,5),
array(2,3,4,5,9),
array(1,2,3,7,9),
array(1,3,4,5,7),
array(2,3,4,7,9),
array(2,4,5,10,29),
array(1,8,10,11,15) // etc.
);
如何获得
$n
数字的组合(数组),其中该组合(或者更确切地说,由该$n数字组合生成的5个数字组合)将与
$myArray
的大多数子数组相匹配

示例:对于
$myArray
而言,
$n=7
所需的结果将是
数组(1,2,3,4,5,7,9)
,因为从该结果中总共衍生出二十一个5位数组合:

1,2,3,4,5
1,2,3,4,7
1,2,3,4,9
//... and so on
这些组合将匹配几乎所有的子数组(只有第二个和最后两个子数组超出范围)

我尝试过使用
数组\u count\u values()计数,但在这种情况下,所有数字的简单频率不起作用…

类组合实现了迭代器
                        class Combinations implements Iterator
                        {
                            protected $c = null;
                            protected $s = null;
                            protected $n = 0;
                            protected $k = 0;
                            protected $pos = 0;

                            function __construct($s, $k) {
                                if(is_array($s)) {
                                    $this->s = array_values($s);
                                    $this->n = count($this->s);
                                } else {
                                    $this->s = (string) $s;
                                    $this->n = strlen($this->s);
                                }
                                $this->k = $k;
                                $this->rewind();
                            }
                            function key() {
                                return $this->pos;
                            }
                            function current() {
                                $r = array();
                                for($i = 0; $i < $this->k; $i++)
                                    $r[] = $this->s[$this->c[$i]];
                                return is_array($this->s) ? $r : implode('', $r);
                            }
                            function next() {
                                if($this->_next())
                                    $this->pos++;
                                else
                                    $this->pos = -1;
                            }
                            function rewind() {
                                $this->c = range(0, $this->k);
                                $this->pos = 0;
                            }
                            function valid() {
                                return $this->pos >= 0;
                            }
                            //
                            protected function _next() {
                                $i = $this->k - 1;
                                while ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i)
                                    $i--;
                                if($i < 0)
                                    return false;
                                $this->c[$i]++;
                                while($i++ < $this->k - 1)
                                    $this->c[$i] = $this->c[$i - 1] + 1;
                                return true;
                            }
                        }







            $tickets = array(
                 array(1,2,3,4,5),
                 array(5,6,10,18,20),
                 array(1,2,3,4,5),
                 array(2,3,4,5,9),
                 array(1,2,3,7,9),
                 array(1,3,4,5,7),
                 array(2,3,4,7,9),
                 array(2,4,5,10,29),
                 array(1,8,10,11,15) // etc.
            );
    // first we need to find all numbers  that are actually in one of the arrays.

            foreach($tickets as $anArray) {
                foreach($anArray as $aNumberUsed){
                    $numbersUsed[$aNumberUsed] = $aNumberUsed;
                } 
            }
  // next we assign the number of integers in the set we are looking for.       
            $r = 7;
// next we run the above class on our array (which gets us all of the possible combinations of these numbers).
            foreach(new Combinations($numbersUsed, 7) as $comboKey => $substring){
                $comboList[$comboKey] = $substring;
                $countWins = 0;
// here we loop through all of the 5 number arrays, and flag any array who has all the variables in this iteration of the possible numbers.  There are cleaner ways to do this, but this is easy to understand.
                foreach($tickets as $valueList) {
                    $countNumbersFound = 0;
                    foreach($valueList as $value) {
                        if(in_array($value, $substring)) {
                            $countNumbersFound++;
                        }
                    }
                    if($countNumbersFound == 5) {
                        $countWins++;
                    }       
                }
                $foundCount[$comboKey] = $countWins;
            }
    $bigly = max($foundCount);


    $key = array_search($bigly, $foundCount);

    foreach($comboList[$key] as $wellDone) {
        echo "$wellDone ,";
    }   
{ 受保护的$c=null; 受保护的$s=null; 受保护的$n=0; 受保护$k=0; 受保护的$pos=0; 函数构造($s,$k){ if(is_数组($s)){ $this->s=array\u值($s); $this->n=计数($this->s); }否则{ $this->s=(字符串)$s; $this->n=strlen($this->s); } $this->k=$k; $this->revind(); } 函数键(){ 返回$this->pos; } 函数电流(){ $r=数组(); 对于($i=0;$i<$this->k;$i++) $r[]=$this->s[$this->c[$i]]; 返回为数组($this->s)$r:内爆(“”,$r); } 函数next(){ 如果($this->_next()) $this->pos++; 其他的 $this->pos=-1; } 函数倒带(){ $this->c=范围(0,$this->k); $this->pos=0; } 函数valid(){ 返回$this->pos>=0; } // 受保护函数_next(){ $i=$this->k-1; 而($i>=0&&$this->c[$i]==$this->n-$this->k+$i) $i--; 如果($i<0) 返回false; $this->c[$i]++; 而($i++<$this->k-1) $this->c[$i]=$this->c[$i-1]+1; 返回true; } } $tickets=数组( 阵列(1,2,3,4,5), 阵列(5,6,10,18,20), 阵列(1,2,3,4,5), 阵列(2,3,4,5,9), 阵列(1,2,3,7,9), 阵列(1,3,4,5,7), 阵列(2,3,4,7,9), 阵列(2,4,5,10,29), 数组(1,8,10,11,15)//等。 ); //首先,我们需要找到其中一个数组中的所有数字。 foreach($anArray){ foreach($anArray为$aNumberUsed){ $numberssused[$aNumberUsed]=$aNumberUsed; } } //接下来,我们在寻找的集合中分配整数的数量。 $r=7; //接下来,我们在数组上运行上面的类(这将获得这些数字的所有可能组合)。 foreach(新组合($numbersUsed,7)作为$comboKey=>$substring){ $comboList[$comboKey]=$substring; $countWins=0; //在这里,我们循环遍历所有5个数字数组,并标记在这个可能的数字迭代中具有所有变量的任何数组。有更简洁的方法可以做到这一点,但这很容易理解。 foreach($valueList形式的门票){ $countNumbersFound=0; foreach($valueList作为$value){ if(在数组中($value,$substring)){ $countNumbersFound++; } } 如果($countNumbersFound==5){ $countWins++; } } $foundCount[$comboKey]=$countWins; } $bigly=max($foundCount); $key=array\u search($bigly,$foundCount); foreach($comboList[$key]作为$wellDone){ 回声“$干得好,”; }
这门课是从这里偷来的:

下课后的一切都是原创的。我不相信重新发明轮子。

不,我没有
数组(1,2,3,4,5,7,9)
-这就是我想要的,期望的结果->我想要的组合将覆盖大部分子数组。。。对于
$myArray
来说,就是这样的组合-但我不知道如何计算。你的导师