生成单词组合

时间:2011-06-14 11:42:29

标签: php word

例如,这是我的文字:

$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,但我无法生成如表现。有没有机会像我的请求一样生成?

5 个答案:

答案 0 :(得分:14)

您可以查看此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

答案 1 :(得分:6)

shuffle($array);
echo join(' ', array_slice($array, 0, mt_rand(1, count($array))));

这为您提供一个随机“句子”。根据需要重复。

答案 2 :(得分:3)

function pc_permute($items, $perms = array( )) {
    if (empty($items)) {
        print join(' ', $perms) . "\n";
    } else {
        for ($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' ) );

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

答案 3 :(得分:3)

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
)

答案 4 :(得分:0)

对于所有可能的组合我改进了@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
)