确定给定数组的名称的所有可能性的算法 [在 PHP 中]


Algorithm to determine all possibilities for names given an array [in PHP]?

此处的代码与项目中的代码不匹配导致问题。

foreach ($words as $first) {
    foreach ($words as $second) {
        $taken[] = $first.$second;
    }
}

如果你想要所有可能的组合没有重复项(例如:w1w1),请使用这个:

$allCombinations = null;
$words = array('w1', 'w2', 'w3', 'w4', 'w5', 'w6', 'w7', 'w8', 'w9', 'w10');
foreach ($words as $word1)
{
    foreach ($words as $word2)
    {
        if ($word1 != $word2)
        {
            $allCombinations[] = $word1.$word2;
        }
    }
}
echo count ($allCombinations); // The count is 90, and that's what it should be.
print_r($allCombinations);