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

8
  • 2
    Why exactly is 1x and 1y not correct? Commented Nov 18, 2009 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. Commented Nov 18, 2009 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. Commented Nov 18, 2009 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? Commented Nov 18, 2009 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')); Commented Nov 18, 2009 at 16:20

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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 :)
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];
    }
}

1 Comment

I doubt the array(s) will have a fixed length.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.