生成单词组合

|| 例如,这是我的文字:
$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,但无法生成显示的图像。有没有机会像我的要求那样生成?     
已邀请:
        你可以看看这个梨包装 用法示例:
<?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
    
        
shuffle($array);
echo join(\' \', array_slice($array, 0, mt_rand(1, count($array))));
这给了您一个随机的“句子”。根据需要重复。     
         函数pc_permute($ items,$ perms = array()){     如果(空($ items)){         打印join(\'\',$ perms)。 \“ \\ n \”;     }其他{         对于($ i = count($ items)-1; $ i> = 0;-$ i){             $ newitems = $ items;             $ newperms = $ perms;             list($ foo)= array_splice($ newitems,$ i,1);             array_unshift($ newperms,$ foo);             pc_permute($ newitems,$ newperms);         }     } } 来源:http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm#phpckbk-CHP-4-EX-6 还要检查该网站上的下一个示例 例如:
pc_permute( explode( \' \', \'buy new microsoft windows\' ) );
购买新的Microsoft Windows 新购买Microsoft Windows 购买Microsoft新视窗 微软购买新视窗 新的Microsoft购买Windows 微软新购买Windows 购买新的Windows微软 新购买的Windows微软 购买Windows新版Microsoft Windows购买新的微软 新窗口购买微软 Windows新购买微软 购买新的Microsoft Windows 微软购买新的Windows 购买Windows Microsoft新 Windows购买微软新 Microsoft Windows购买新 Windows Microsoft购买新 新的Microsoft Windows购买 微软新视窗购买 新视窗Microsoft购买 Windows新微软购买 Microsoft Windows新购买 Windows Microsoft新购买     
        从php.net shuffle doc找到
function powerSet($in,$minLength = 1) { 
   $count = count($in); 
   $members = pow(2,$count); 
   $return = array(); 
   for ($i = 0; $i < $members; $i++) { 
      $b = sprintf(\"%0\".$count.\"b\",$i); 
      $out = array(); 
      for ($j = 0; $j < $count; $j++) { 
         if ($b{$j} == \'1\') $out[] = $in[$j]; 
      }
      $out_val = implode(\" \", $out); 
      if (count($out) >= $minLength) { 
         $return[] = $out_val; 
      } 
   } 
   return $return; 
}

print_r(powerSet($str_arr));
结果是
Array
(
    [0] => windows
    [1] => microsoft
    [2] => microsoft windows
    [3] => new
    [4] => new windows
    [5] => new microsoft
    [6] => new microsoft windows
    [7] => buy
    [8] => buy windows
    [9] => buy microsoft
    [10] => buy microsoft windows
    [11] => buy new
    [12] => buy new windows
    [13] => buy new microsoft
    [14] => buy new microsoft windows
)
    
        对于所有可能的组合,我都改进了@SujitAgarwal答案: 下载Math_Combinatoeics。
include_once \'Combinatorics.php\';

$words = array(\'buy\',\'microsoft\',\'windows\');
$count = count($words);
$return = array();

$combinatorics = new Math_Combinatorics;

for ($num=1; $num <= $count; $num++ ){
  foreach($combinatorics->permutations($words, $num) as $p) {
    $return[] = join(\' \', $p);
  }
} 

print_r($return);
输出:
Array
(
  [0] => buy
  [1] => microsoft
  [2] => windows
  [3] => buy microsoft
  [4] => microsoft buy
  [5] => buy windows
  [6] => windows buy
  [7] => microsoft windows
  [8] => windows microsoft
  [9] => buy microsoft windows
  [10] => buy windows microsoft
  [11] => microsoft windows buy
  [12] => microsoft buy windows
  [13] => windows buy microsoft
  [14] => windows microsoft buy
)
    

要回复问题请先登录注册