2017-05-23 5 views
0

$myArray은 항상 5 개의 숫자가 포함 된 하위 배열로되어 있습니다. 숫자는 크기별로 정렬되어 있으며 하위 배열에서 반복 될 수 없지만 더 많은 "동일한"하위 배열이있을 수 있습니다 (하위 배열은 동일한 번호)를 $myArray에 입력하십시오. 나는이 조합 (또는이 $ N-숫자의 조합에서 발생 오히려 5 숫자 조합) $myArray의 하위 배열을 최대한 일치 $n 번호 (배열)을 조합하여 얻을 수있는 방법PHP : 하위 배열과 가장 많이 일치하는 조합을 얻는 방법?

$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. 
); 

?

:이 결과에서 파생 된 총 이십일 5 - 숫자 조합이 있기 때문에 $myArray에 대한 $n=7에 대해 원하는 결과는 array(1,2,3,4,5,7,9) 것 :

1,2,3,4,5 
1,2,3,4,7 
1,2,3,4,9 
//... and so on 

는 이러한 조합은 거의 일치합니다 모든 하위 배열 (두 번째 및 마지막 두 개의 하위 배열 만 범위를 벗어남).

나는

답변

0
     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 ,"; 
    } 

클래스는 노골적으로 여기에서 도난 ... array_count_values()하지만이 경우에는 작동하지 않는 모든 숫자의 간단한 주파수 카운트 시도 : http://www.developerfiles.com/combinations-in-php/

모두를 수업 후 원래입니다. 나는 바퀴를 다시 발명하는 것을 믿지 않는다.

+0

아니요,'array (1,2,3,4,5,7,9)'가 없습니다 - 원하는 결과입니다.> 원하는 조합을 원합니다. 하위 배열 ... 그리고'$ myArray'의 경우이 조합입니다. 그러나 어떻게 이해해야 할 지 모르겠습니다. – GilesNorthcott

+0

강사가이 코드에 만족해야합니다. 특히 목록 값과 값을 비교하기 위해 반복되는 구조를 정리할 경우 특히 그렇습니다. – kyle

+0

이 기능을 사용하셨습니까? – kyle