私はデータベースから単語のリストを取得しようとしていますが、$ word1。$ word2。$ word3の形式で独自の3単語の組み合わせを作成し、それを星に割り当てます。単語の表からランダムに3単語の組み合わせが重複しないようにするにはどうすればよいですか?
私は重複した組み合わせを避けたい - 私は、各星に一意の3語の識別子を持たせたい。
私の現在の方法では、すべての可能な3語の組み合わせの配列を作成し、それから星に割り当てられた配列から各組み合わせを削除します。しかし、私の単語リストには数千語を使うつもりです。つまり、この配列には数千億もの組み合わせが含まれていることになるので、この方法は非常に非効率的です。
これをより効果的に達成するにはどうすればよいですか?私の最初の考えは、私は各星をループし、3ワードの組み合わせを作成して割り当て、次に配列にコンボを追加し、各星について新しく生成されたコンボが配列に含まれているかどうかを確認することです。
コード
<?php
// Initiate connection to the database...
$db = mysqli_connect('localhost', 'root', '', 'stellar');
// Query database of words
$words_sql = "SELECT * FROM words";
$words_res = mysqli_query($db, $words_sql)or die(mysqli_error());
// Create array of words
$words = array();
// Loop through each word from the database and add each to an array
while($row = mysqli_fetch_array($words_res)){
$words[] = $row['word'];
}
// Create array of all possible three-word combinations, from which we will randomly select our combinations
$triplets = array();
foreach ($words as $word1){
foreach ($words as $word2){
foreach($words as $word3){
if ($word1 !== $word2 && $word2 !== $word3 && $word1 !== $word3){
$triplets[] = "$word1.$word2.$word3";
}
}
}
}
// 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 and star_id in variables
$star = $row['star_name'];
$star_id = $row['star_id'];
// Set $three_words as a random combination from the array of possible combinations...
$ran_num = array_rand($triplets);
$three_words = $triplets[$ran_num];
// ...and remove this particular combination, in order to prevent repating combinations
array_splice($triplets, $ran_num, 1);
// Attach the random 3-word combination to the star
echo $star.' '.$three_words.'<br/><br/>';
}
?>
1000語の場合、1000000000通りの組み合わせがあります。名前を付ける必要のある星の総数と比較してどのように表示されますか?たとえば、100万個以上の星が500000000個ありますか? – apokryfos
私は〜250万星から始めたいので、〜300単語しか必要ないと思います。 – Callum