0

I'm trying something like

    $a=array("aaa","bbb","cccc","dddd");
$b=array();
foreach($a as $k=>$v){ 
$n=sizeof($a);
for($i=($k+1);$i<=$n;$i++) $b[]=$a[$k].$a[$i];
}
var_dump($b);

And I would like to have all kinds of glued strings in $b regardless of the array length of $a. I've tried different for loops which served partial solutions, maybe I'm not seeing the forest for the trees?

If I closer I'll post the upgraded code. Thanks for helping.

Rápli András
  • 3,869
  • 1
  • 35
  • 55
  • So you're either looking for all `permutations` or for a `powerset` of the array. – GolezTrol Aug 13 '13 at 20:04
  • possible duplicate of [Finding cartesian product with PHP associative arrays](http://stackoverflow.com/questions/6311779/finding-cartesian-product-with-php-associative-arrays) –  Aug 13 '13 at 20:04
  • One string in the array can not occur twice in the result. – Rápli András Aug 13 '13 at 20:06

1 Answers1

0

You should take a look at the Cartesian Product Algorithm. It will generate every combination from the supplied arrays.

Community
  • 1
  • 1
Matthew R.
  • 4,332
  • 1
  • 24
  • 39