当前位置: 动力学知识库 > 问答 > 编程问答 >

php - combinations: avoiding multiple nested foreach

问题描述:

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?

网友答案:

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?

网友答案:

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.

网友答案:

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?

网友答案:

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'.

分享给朋友:
您可能感兴趣的文章:
随机阅读: