2016-07-17 67 views
0

这里我的我的用于产生三个词的组合(combo_generator.php)代码:如何防止重复组合两个随机数字?

<?php 

include "db_connect.php"; 

// Query database of words 
$words_sql = "SELECT * FROM words"; 
$words_res = mysqli_query($db, $words_sql)or die(mysqli_error()); 

// Create array of words 
$array = array(); 

// Loop through each word and add each to the array 
while($row = mysqli_fetch_array($words_res)){ 
    $array[] = $row['word']; 
} 

// Set $word1 as random word from $array 
$ran_num = array_rand($array); 
$word1 = $array[$ran_num]; 

// Remove the chosen word from the array 
array_splice($array, $ran_num, 1); 

// Reset the array 
$array2 = $array; 

// Set $word2 as random word from $array 
$ran_num2 = array_rand($array2); 
$word2 = $array2[$ran_num2]; 

// Set variables 
$word  = 'star'; 
$three_words = "$word.$word1.$word2"; 

echo $three_words; 

?> 

这是我的用于分配随机三字组合从数据库(test.php的)每个星号代码:

<?php 

include "db_connect.php"; 

// Pull all stars from database 
$stars_sql = "SELECT * FROM stars"; 
$stars_res = mysqli_query($db, $stars_sql)or die(mysqli_error()); 

// Loop through every star in the array 
while($row = mysqli_fetch_array($stars_res)){ 
    // Store the star name in a variable 
    $star = $row['star_name']; 

    // Create a random 3-word combination 
    include "combo_generator.php"; 

    // Attach the random 3-word combination to the star 
    echo $star.'&nbsp;&nbsp;&nbsp;&nbsp;'.$three_words.'<br/><br/>'; 
} 

?> 

我试图生成形式star.word1.word2,其中字词1是从数据库中随机单词,单词2是从数据库(字词不同的随机字三个词组合!= WORD2)。然后将生成的每个组合分配给数据库中的星形。

如何避免重复组合?通过重复组合,我的意思是:

组合1:star.lamp.chair;组合2:star.dog.ball;组合3:star.ball.dog;组合4:star.lamp.chair

在这里,组合1和组合4是相同的 - 这种重复是我想要避免的。顺序并不重要,所以组合2和组合3都很好。

+0

你能提供您使用的代码重复的过程?必须有一些循环或请求发生才能获得多个请求? – trincot

+0

或'shuffle'数组和'array_slice' – splash58

+0

我假设你从某种应用程序多次调用此脚本以获得多个组合?如果是这样,您可以保存所有已经生成的组合,并检查新组合是否与其中任何组合相同。如果是,则创建一个新组合。 – GregaMohorko

回答

1

您可以创建与所有可能的字对的数组,然后做拾删除该数组:

<?php 

// Retrieve a list of words from the DB and build the pairs list 
include "db_connect.php"; 

// Query database of words 
$words_sql = "SELECT * FROM words"; 
$words_res = mysqli_query($db, $words_sql)or die(mysqli_error()); 

// Create array of words 
$array = array(); 

// Loop through each word and add each to the array 
while($row = mysqli_fetch_array($words_res)){ 
    $array[] = $row['word']; 
} 

// Create all valid pairs 
$pairs = array(); 
foreach ($array as $word1) { 
    foreach ($array as $word2) { 
     if ($word1 !== $word2) { 
      $pairs[] = "$word1.$word2"; 
     } 
    } 
} 

// Pull all stars from database 
$stars_sql = "SELECT * FROM stars"; 
$stars_res = mysqli_query($db, $stars_sql)or die(mysqli_error()); 

// Loop through every star in the array 
while($row = mysqli_fetch_array($stars_res)){ 
    // Store the star name in a variable 
    $star = $row['star_name']; 

    // Set $pair as random word from $pairs 
    $ran_num = array_rand($pairs); 
    $pair = $pairs[$ran_num]; 

    // ... and remove it 
    array_splice($pairs, $ran_num, 1); 

    // Set variables 
    $word  = 'star'; 
    $three_words = "$word.$pair"; 

    // Attach the random 3-word combination to the star 
    echo $star.'&nbsp;&nbsp;&nbsp;&nbsp;'.$three_words.'<br/><br/>'; 
} 

?> 
+0

如果有很多单词,这可能会很慢。如果所需组合的数量很少,那就没有必要。检查我的答案。 – GregaMohorko

+0

这两个答案都是可行的,并有其优点和缺点。 – trincot

+0

当然,我只是想说明一下。如果有很多单词和很多需要的组合,你的答案会更好。 – GregaMohorko

2

您可以保存已创建的组合。

创建一个全局数组(=在test.php的一个变量)代表所有已创建的组合:

/*@var $createdCombinations array*/ 
$createdCombinations=[]; 

然后,之前只是呼应你的新组合,检查它是否已被创建。如果没有,将它添加到数组中。如果是,则创建一个新组合。

像这样:

// ... 

while(true){ 
    // Set $word1 as random word from $array 
    $ran_num = array_rand($array); 
    $word1 = $array[$ran_num]; 

    // ... 

    if(!in_array($three_words,$createdCombinations)){ 
     $createdCombinations[]=$three_words; 
     echo $three_words; 
     break; 
    } 
} 

代码的其他部分可以保持不变,所以他们被省略。

+0

我测试了这个,它工作 - 谢谢。 – Callum

+0

不客气。请注意,如果您有大量单词和几个需要的组合,这个答案实际上比接受的答案好。接受的答案在开始时会花费很多时间。但是这个对每个组合都需要一点时间。所以如果你只会采取少量的组合,这个答案比接受的答案要快。但是如果你会采取很多组合(1000+),那么接受的答案会更快。 – GregaMohorko

+0

感谢您的解释。我的意图是使用一个庞大的单词数据库,其中没有。组合>不。的话。出于这个原因,我接受了trincot的回答,正如你所慷慨地建议的那样:-)。 – Callum