php – 生成单词组合

例如这是我的文字:

$str = 'buy new microsoft windows';

我用数组爆炸文本和列表:

Array
(
    [0] => buy
    [1] => new
    [2] => microsoft
    [3] => windows
)

我想在数组中生成如下所示的内容:

buy new
buy new windows
buy microsoft 
buy microsoft windows
buy windows
new microsoft
new microsoft windows
new windows
microsoft windows

我用foreach和rand尝试,但我不能像显示那样生成.有没有机会像我的要求一样产生?

最佳答案
你可以看看这个PEAR PACKAGE
使用示例

<?php
require_once 'Math/Combinatorics.php';
$words = array('buy', 'new', 'microsoft');
$combinatorics = new Math_Combinatorics;
foreach($combinatorics->permutations($words, 2) as $p) {
  echo join(' ', $p), "\n"; 
}

输出结果为:

buy new
new buy
buy microsoft
microsoft buy
new microsoft
microsoft new

转载注明原文:php – 生成单词组合 - 代码日志