0

Im using the accepted answer in this question. As I have the same requirements, I need to get all combinations of an array of variable length with a variable number of elements. However I also need it to produce all combinations which don't use all the elements of the array, but are in order. If that makes sense?

So if this is the array:

$array = array(
    array('1', '2'),
    array('a', 'b', 'c'),
    array('x', 'y'),
);

I also want it to add like 1a, 1b, 1c, 2a, 2b, 2c. But not 1x or 1y, because it misses out the second element of the array.

I can't quite figure out how to change the answer to include this.

Thanks, Psy

Community
  • 1
  • 1
Psytronic
  • 6,043
  • 5
  • 37
  • 56
  • 2
    Why exactly is 1x and 1y not correct? – Ikke Nov 18 '09 at 14:30
  • @Ikke: Because it doesn't contain anything from the second element of the array, ie a, b or c. @Bart: Sorry, I should have explained the situation a little better, however I'm in the same situation as what the OP of the linked post was after, but an additional bit, changed the question to hopefully reflect this. – Psytronic Nov 18 '09 at 15:02
  • I read the other post already but my questions were not answered by it. That's why I asked. So let me ask again, but now with an example. Can you explain what combinations you are looking for when your array looks like this: `$array = array(array('1', '2'), array('a', 'b', 'c'), array('m', 'n'), array('x', 'y'));` Please try to avoid terms like "etc.", enumerate all combinations please. – Bart Kiers Nov 18 '09 at 15:14
  • Ah, ok, no problem, I'm doing some data mining on some tracking our site performs on our users journey throughout the website, I have collaborated all the data, and got an array consisting of x amount of clicks a user does on our site, and the top y elements they have clicked on (buttons,inputs, links) for that stage of the journey. I now need to get the patterns out of this, by working out each possible combination a person could have taken from these results. Hope that helps? – Psytronic Nov 18 '09 at 15:58
  • Psytronic, who are you answering here? I assume it's not me... If so, you didn't answer my question at all. What output would you like for the array: `$array = array(array('1', '2'), array('a', 'b', 'c'), array('m', 'n'), array('x', 'y'));` – Bart Kiers Nov 18 '09 at 16:20
  • Sorry, based on that array this is what I'm after: $results = array( '1', '1a', '1am', '1an', '1amx', '1amy', '1anx', '1any', '1b', '1bm', '1bn', '1bmx', '1bmy', '1bnx', '1bny', '1c', '1cm', '1cn', '1cmx', '1cmy', '1cnx', '1cny', '2a', '2am', '2an', '2amx', '2amy', '2anx', '2any', '2b', '2bm', '2bn', '2bmx', '2bmy', '2bnx', '2bny', '2c', '2cm', '2cn', '2cmx', '2cmy', '2cnx', '2cny' ); – Psytronic Nov 19 '09 at 08:38
  • So the single `2` should be omitted? Why? What if the first array holds, say, 4 elements? – Bart Kiers Nov 19 '09 at 11:36
  • Err the 2 should have been there, I must have missed it out. If the first element went up to 4 then it would be repeated with 3's and 4's instead of 1s and 2s – Psytronic Nov 19 '09 at 21:23

3 Answers3

1

Using Josh Davis' approach in the answer to the linked question:

    $array = array( array('1', '2'), 
                    array('a', 'b', 'c'), 
                    array('m', 'n'), 
                    array('x', 'y'));

    $result = array();
    $php = 'list($a' . implode(',$a', array_keys($array)) . ')=$array;';
    $close_brakets='';
    $r='';
    foreach($array as $k => $v)
    {
        $r .= '$v'.$k;
        $php.='foreach($a'.$k.' as $v'.$k.'){ $result[]="'.$r.'";';
        $close_brakets.="}";
    }

    $php .= $close_brakets;

    eval($php);

    print_r($result);

gives you the desired combinations

danii
  • 5,553
  • 2
  • 21
  • 23
  • Thanks must not have tried that code, I actually managed to take my current code and change it in the end, however this does work aswell :) – Psytronic Nov 20 '09 at 16:07
0

Something like this? The idea is to loop one array, and combine with each value in another array.

// Loop array[0].
for($i=0; $i<count($array[0]); $i++) {
    // Loop array[1]
    for($j=0; $j<count($array[1]); $j++) {
        echo $array[0][$i];
        echo $array[1][$j];
    }
}
Kristoffer Bohmann
  • 3,986
  • 3
  • 28
  • 35
0

Well taking the code that I was originally using, this is what I came up with, just incase anyone else is curious

$patterns_array = array();

$php = '';
foreach ($patterns as $i = > $arr)
{
    $php .= 'foreach ($patterns[' . $i . '] as $k' . $i . ' => $v' . $i . '){';
    $tmp = array();
    for($ii=1; $ii<=$i; $ii++){
        $tmp[] = $ii; 
    }
    $php .= '$patterns_array[] = $v'.implode('."::".$v', $tmp).';';
}

$php .= '$patterns_array[] = $v' . implode('."::".$v', array_keys($patterns)) . ';' . str_repeat('}', count($patterns));

eval($php);
Psytronic
  • 6,043
  • 5
  • 37
  • 56