Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use recursion, or just straight nested loops for this, but when working with combinations or permutations, the total number of possibilities can explode and become a huge number, consuming lots of memory to the point where you just cant run the code. Using an iterator is a nice way to trade cpu efficiency for memory efficiency. Here's an iterator I wrote.</p> <pre><code>class CartesianProductIterator implements Iterator { protected $iterators; function __construct(array $iters) { $this-&gt;iterators = $iters; } function rewind() { foreach ($this-&gt;iterators as $it) { $it-&gt;rewind(); } } function current() { $values = array(); foreach ($this-&gt;iterators as $it) { $values[] = $it-&gt;current(); } return $values; } function key() { return null; } function next() { /* loop them in reverse, but exclude first why? example, odometer: 55199 you always check the rightmost digit first to see if incrementing it would roll it over and need to be "rewound" to 0, which causes the digit to the left to increase as well, which may also cause it to roll over as well, and so on... looping in reverse operates from right column to the left. we dont rewind the first column because if the leftmost column is on its last element and needs to roll over then this iterator has reached its end, and so rewind() needs to be explicitly called */ for ($i = count($this-&gt;iterators) - 1; $i &gt; 0; --$i) { $it = $this-&gt;iterators[$i]; $it-&gt;next(); if ($it-&gt;valid()) { // were done advancing because we found a column that didnt roll over return; } else { $it-&gt;rewind(); } } //if execution reached here, then all of the columns have rolled over, so we must attempt to roll over the left most column $this-&gt;iterators[0]-&gt;next(); } function valid() { return $this-&gt;iterators[0]-&gt;valid(); } } </code></pre> <p>Then use it as </p> <pre><code>$iterators = array(); foreach ($a as $columnNumber =&gt; $values) { $iterators[] = new ArrayIterator($values); } foreach (new CartesianProductIterator($iterators) as $combo) { // combo has 1 value from each of the ArrayIterators we instantiated printf("summing %s = %d\n", join('+', $combo), array_sum($combo)); } </code></pre> <p>heres a demo <a href="http://codepad.org/UasdgvWf" rel="nofollow">http://codepad.org/UasdgvWf</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload