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

时间:2014-11-26 18:30:58

标签: php arrays algorithm

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果你想要所有可能的组合而没有重复项(例如: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);