Click to See Complete Forum and Search --> : all possible combinations of words from 3 arrays


BWWebDesigns
12-12-2007, 03:57 PM
Need help generating all possible combinations of words from 3 arrays

Lets say i have 3 arrays and each has 10 words in them or how ever many etc

I want to be able to generate an array to be used later on in my script that will contain all possible combinations of the words in the following way

Words will either be taken from the first array only, the first array and the second or the first second and third


so if i had arrays like so

$words1 = array("word1","word2");
$words2 = array("word3","word4");
$words3 = array("word5","word6");



The end outcome would generate an array containing the combinations picking one word combinations from all 3 arrays two word combinations from the first two arrays but only in that order and three word combos from all three in order never mixing the order, the array done by hand would look like this, as these are the combinations generated how i want them.

$combinedwords = array("word1","word2","word3","word4","word5","word6","word1-word3","word1-word4",word2-word3","word2-word4","word1-word3-word5","word1-word4-word5",word1-word3-word6","word1-word4-word6","word2-word3-word5","word2-word4-word5","word2-word3-word6","word2-word4-word6");

Now obviously when theres more words in each array the combinations added to the output array would be greatly increased

Then with the array that is now full of all combinations i can do things with them later in my script but i already have that sorted

Any ideas on how i would do this?


Thanks in a dvance

SyCo
12-14-2007, 06:01 PM
This will produce the array although not in you example's order.
<?
$words1 = array("word1","word2");
$words2 = array("word3","word4");
$words3 = array("word5","word6");

foreach($words1 as $k1 =>$v1){
$all_words[]=$v1;
foreach($words2 as $k2 => $v2){
$all_words[]=$v2;
$all_words[]=$v1."-".$v2;
foreach($words3 as $k3 => $v3){
$all_words[]=$v3;
$all_words[]=$v1."-".$v2."-".$v3;
}
}
}
$all_words=array_unique($all_words);


//echoing the array
foreach($all_words as $v){
echo $v."<br>";
}


You'll get this.

word1
word3
word1-word3
word5
word1-word3-word5
word6
word1-word3-word6
word4
word1-word4
word1-word4-word5
word1-word4-word6,
word2
word2-word3
word2-word3-word5
word2-word3-word6
word2-word4
word2-word4-word5
word2-word4-word6

Hope that works for you :)

(edit: Looking at it now, I thought I'd need the key values but i didn't so you could ditch the $kx from the foreachs if you wanted to.)

BWWebDesigns
12-14-2007, 06:49 PM
Thanks that worked great

i changed your last section that displays them and repalced it with this

sort($all_words);

//echoing the array
foreach($all_words as $i => $v){
echo $all_words[$i]."<br>";
}

Now they are sorted into alphabetical order and look a bit neater and will work better for what i need

But thanks for the script it was a great help, it was so simple, i always get stumped by the simple stuff lol

SyCo
12-17-2007, 09:08 AM
You're welcome!
:)