For example this is my text :
$str = 'buy new microsoft windows';
I explode text and list with array :
Array
(
[0] => buy
[1] => new
[2] => microsoft
[3] => windows
)
I want to generate words in array to something like this:
buy new
buy new windows
buy microsoft
buy microsoft windows
buy windows
new microsoft
new microsoft windows
new windows
microsoft windows
I tried with foreach and rand but I couldn't generate like showed. Is there any chance to generate just like my request?
Found from 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));
and the results will be,
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
)
You can have a look at this PEAR PACKAGE Example usage:
<?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";
}
The output will be:
buy new
new buy
buy microsoft
microsoft buy
new microsoft
microsoft new
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With