Forum Moderators: coopster

Message Too Old, No Replies

Combining arrays into combinations?

         

erikcw

6:32 pm on Dec 28, 2005 (gmt 0)

10+ Year Member



I have some code that combines 3 arrays of data into strings of different combinations.

IE:


$words1 = "1
2
3
4";
$words2 = "a
b
c
d";
$words3 = "x
y
z";

The Output is:
1
1a
1ax
1b
1bx
etc...

I want to add some options where if a condition is true, then it will only output data that has a combination from all 3 arrays OR at least 2 arrays OR all possible combinations.

Basically I'm trying to setup it up so I can have the function only output "1ax" but NOT "1" - or "1b" AND "1ax" but NOT "1"...

Here is my logic:


if ($all3 == 1) {
output all possible combinations (code below)
}
if ($min3 == 1) {
output all combinations that have data from each of the 3 arrays;
}
if ($min2 == 1) {
output all combinations that have data from at least 2 of the arrays
}


$words1 = array();
$words2 = array();
$words3 = array();

$words1 = explode("\n", $_POST['list_a']);
$words2 = explode("\n", $_POST['list_b']);
$words3 = explode("\n", $_POST['list_c']);

$combinations = array();
foreach ($words1 as $word1)
{
foreach ($words2 as $word2)
{
foreach ($words3 as $word3)
{
$combinations[] = trim($word1) . ' ' . trim($word3);
$combinations[] = trim($word1) . ' ' . trim($word2) . ' ' . trim($word3);
}

Can anyone tell me how I can modify this code to accomplish that?

Thanks!

erikcw

5:23 am on Jan 3, 2006 (gmt 0)

10+ Year Member



any ideas? This is starting to drive me nuts!

jatar_k

6:17 pm on Jan 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



do you need every possible combo every time?

if you take the 1 and 3 line out of your inner most loop won't it work? Haven't tested yet, still getting through my first coffee.

erikcw

5:42 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



Removing the "1 and 3" line would only eliminate the 2 word combination. How do I get rid of the 1 word version?

erikcw

5:52 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



Never mind - this loop only creates 2 and 3 keyword versions....

Thanks for your help!