2016-07-17 70 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