php - combinations: avoiding multiple nested foreach -
How can you avoid nesting frames when you need to combine / check array elements? Example code:
$ as = array ($ optionA1, $ optionA2) $ bs = array ($ optionB1, $ optionB2) $ cs = array ($ optionC1, $ OptionC2) foreach ($ as $ A) {foreach ($ bs $ b) {foreach (c $ as c $) {$ result = $ this- & gt; Method ($ a, $ b, $ c); If ($ result) etc}}}
Any option that can avoid nesting?
You can write your own Iterator class which implements. After that you can accept three arrays to its constructor and then you can use it with a loop in combination with the foreach
However, I think it will be quite slow, so I I would like to avoid it.
Comments
Post a Comment