Create couples of words in every possible combination from array
My mind is a bit numb at the moment,
suppose i have this array:
Array
(
[0] => foo
[1] => bar
[2] => love
)
I'd like to create couples of possible combinations (minus one val),
such as
foo bar
bar love
foo love
and not their opposite (but that's easier)
bar foo
love bar
love foo
of course the array can be of more elements, and in such cases i'd like to find
Array
(
[0] => foo
[1] => bar
[2] => love
[3] => test
)
foo bar love
bar love test开发者_Python百科
love test foo
foo love test
bar test foo
love test bar
and so on...
any idea?
thanks!
function getAllCombinations($array){
$combinations = array();
foreach ($array as $x)
foreach ($array as $y) {
if($x != $y && !in_array($y.' '.$x,$combinations ))
array_push($combinations, $x.' '.$y);
}
return $combinations ;
}
returns
Array
(
[0] => foo bar
[1] => foo love
[2] => bar love
)
I'll strongly suggest you not to go that route, as the complexity of the algorithm which is required to generate this is O((n)!) so this is not feasible for any arrays larger then 20 elements. Event for array of 10 elements there will be 3628800 possible combinations for array with 15 - 1307674368000 for 100 - 9e+157 (that's a 9 and 157 zeros)
Well I didn't want to, but here it is. This will generate all possible combinations(with n-1 lenght) of any array (length n). I would not recommend to run it with the array length more then 7.
$array = array('foo','bar','love','test');
function getAllCombinations($array)
{
if (count($array)==1)
return ($array);
$res = array();
foreach ($array as $i=>$val)
{
$tArray = $array;
unset($tArray[$i]);
$subRes = getAllCombinations($tArray);
foreach ($subRes as $t)
{
$res[]= $val.' '.$t;
}
}
return $res;
}
foreach ($array as $key=>$val)
{
$tArray = $array;
unset($tArray[$key]);
$res = getAllCombinations($tArray);
foreach ($res as $t)
echo $t.'<br />';
}
If you will use this with only 4 elements in array and without duplicates, here is shorter and faster solution:
$array = array('foo','bar','love','test');
for ($i1=0; $i1<count($array);$i1++)
{
for ($i2=$i1+1; $i2<count($array);$i2++)
{
for ($i3=$i2+1; $i3<count($array);$i3++)
{
echo $array[$i1].' '.$array[$i2].' '.$array[$i3].'<br />';
}
}
}
if you sort initial array alphanumerically, the result will be ordered alphanumerically as well
I am not sure what exactly you want to get, but I think the following should do the job (if you want to do what I think you want):
for($i = 0; $i < sizeof($array); $i++) {
$text = "";
for ($j = 0; $j < sizeof($array); $j++) {
if ($i != $j) {
$text = $text." ".$array[$j];
}
}
echo $text;
}
You can make use of Math_Combinatorics package.
Example Code:
$combinatorics = new Math_Combinatorics;
$set = array(
'one' => 'foo',
'two' => 'bar',
'three' => 'love'
);
$combinations = $combinatorics->combinations($set, 2);
print_r($combinations);
2 is the number of words to be combined.
Output at my end:
Array
(
[0] => Array
(
[one] => foo
[two] => bar
)
[1] => Array
(
[one] => foo
[three] => love
)
[2] => Array
(
[two] => bar
[three] => love
)
)
精彩评论