2016-04-18 14 views
0

zu Finden habe ich ein ArrayWie Wörter Kombination Php

$new_array=array('c','a','m','t','p'); 

Jetzt will ich die Worte Kombination finden, die in der Worttabelle existiert.

Ich habe versucht zu erreichen, aber nicht gelungen.

das ist mein PHP-Code.

     $words = array(); 
       $set = powerSet($new_array,2); 

    $mysql = new mysqli("localhost","root","","startup"); 
$sql = "SELECT wordid from words WHERE lemma = '%s'" ; 

foreach ($set as $key => $value) 
{ 
    $word = implode("", $value); 
    $wordPermutation = permute($word); 

    foreach($wordPermutation as $keyWord) 
    { 
     if(!in_array($keyWord, $words)) 
     { 
      if($result = $mysql->query(sprintf($sql,$keyWord))) 

      { 
        var_dump(sprintf($sql,$keyWord)); 
       if($result->num_rows > 0) 
       { 
        $words[] = $keyWord; 
       } 
      } 
    } 
} 
} 

print_r($words); 


function powerSet($in, $minLength = 1, $max = 10) { 
    $count = count ($in); 
    $members = pow (2, $count); 
    $return = array(); 
    for($i = 0; $i < $members; $i ++) { 
     $b = sprintf ("%0" . $count . "b", $i); 
     $out = array(); 
     for($j = 0; $j < $count; $j ++) { 
      if ($b {$j} == '1') 
       $out [] = $in [$j]; 
     } 
     if (count ($out) >= $minLength && count ($out) <= $max) { 
      $return [] = $out; 
     } 

    } 
    return $return; 
} 


function permute($str) { 
    if (strlen($str) < 2) { 
     return array($str); 
    } 
    $permutations = array(); 
    $tail = substr($str, 1); 
    foreach (permute($tail) as $permutation) { 
     $length = strlen($permutation); 
     for ($i = 0; $i <= $length; $i++) { 
      $permutations[] = substr($permutation, 0, $i) . $str[0] . substr($permutation, $i); 
     } 
    } 
    return $permutations; 
} 

Ich möchte nur die Kombination aus dem Array finden, die in der Tabelle vorhanden ist.

Mein Code abruft alle Kombination

Antwort

0

ich das falsch verstanden haben könnte, aber man konnte nicht eine einzige Datenbankabfrage für diesen Einsatz?

wie:

SELECT wordid FROM `words` WHERE lemma LIKE ("%c%") OR lemma LIKE ("%a%") OR lemma LIKE ("%m%") OR lemma LIKE ("%t%") OR lemma LIKE ("%p%") 

Auf diese Weise erhalten Sie eine Reihe von wordids nach beliebigen Wörtern jede der angegebenen Zeichen erhalten enthält.

Ich würde es als Kommentar hinzufügen, aber nicht genug rep noch.

+0

das ist nicht die allgemeine Lösung für das Problem –