php – 生成单词组合

前端之家收集整理的这篇文章主要介绍了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
原文链接:https://www.f2er.com/php/138166.html

猜你在找的PHP相关文章