combinations: avoiding multiple nested foreach

≯℡__Kan透↙ 提交于 2019-12-10 13:58:36

问题


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:


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?




回答2:


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




回答3:


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.




回答4:


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?



来源:https://stackoverflow.com/questions/1173556/combinations-avoiding-multiple-nested-foreach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!