views:

350

answers:

4

When you need to check/have combinations of array elements, how can you avoid nesting foreach?

Example code:

$as = array($optionA1, $optionA2)
$bs = array($optionB1, $optionB2)
$cs = array($optionC1, $optionC2)

foreach ($as as $a) {
    foreach ($bs as $b) {
      foreach ($cs as $c) {
          $result = $this->method($a, $b, $c);
          if ($result) etc
      }
    }
}

Anyone with alternative approaches that can avoid nesting?

+1  A: 

Have you considered taking the count of each array and multiplying them all together to get a total number of permutations, and then doing a for i to iterate that count? You would have to do some funny counter manipulation for each array, but it should work.

Chris Porter
A: 

You could write your own Iterator class which implements the Iterator interface. You could then have its constructor accept the three arrays and then you can use it to loop over every combination with foreach.

However I think this would be significantly slower, so I would avoid it. It would be interesting to know the reasons you want to avoid the nested foreach loops?

Tom Haigh
It would be nice if whoever down-voted me would add a comment
Tom Haigh
+1  A: 

You haven't really given enough information to know what the alternative is. If you really want to call method() with all combinations of options from $as, $bs and $cs, then nested loops will do the right thing.

Is it the multiply nested loops that bothers you, or the fact that method() is being called count($as) * count($bs) * count($cs) times?

Ether
This answer would make an excellent comment. :)
bzlm
@bzlm: I claim ignorance! I was a SO n00b back then! :)
Ether
+1  A: 

Logically, you have to iterate through each item somehow. You're just shuffling around the process.

If multiple for loops look ugly, maybe you should put your arrays into their own classes, that have their own encapsulated 'checks'.

Rob Elliott