Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ah, nice problem. I've actually had to re-implement this on a number of occasions. This ought to work for you:</p> <pre><code> class Counter { private $bases; private $currNum; private $increment; private $maxVal; public function __construct($bases) { $this-&gt;bases = $bases; $this-&gt;maxVal = 1; $this-&gt;currNum = array(); foreach ($bases as $base) { $this-&gt;maxVal *= $base; $this-&gt;currNum[] = 0; } $this-&gt;increment = 0; } public function increment() { ++$this-&gt;increment; for ($i = count($this-&gt;currNum) - 1; $i &gt; -1; --$i) { $val = $this-&gt;currNum[$i] + 1; if ($val &gt;= $this-&gt;bases[$i]) { $this-&gt;currNum[$i] = 0; } else { $this-&gt;currNum[$i] = $val; return; } } } // TODO handle overflows public function hasNext() { return $this-&gt;increment &lt; $this-&gt;maxVal; } public function getNum() { return $this-&gt;currNum; } public function getIncrement() { return $this-&gt;increment; } } // your sample arrays $arrays = array( array('a', 'b', 'c'), array('d', 'e', 'f'), array('g', 'h', 'i') ); // parameter to counter changes based on how many arrays you have // if you have 4 arrays of len 4, it'll be $counter = new Counter(array(4,4,4,4)); // it'll work with arrays of varying lengths as well. // so if you have 1 array of len 2, another of len 3 and a third of len 4: // $counter = new Counter(array(2,3,4)); $counter = new Counter(array(3,3,3)); $result = array(); while ($counter-&gt;hasNext()) { $indexes = $counter-&gt;getNum(); //print_r($indexes); $result[] = array(); foreach ($indexes as $arr =&gt; $index) { $result[count($result) - 1][] = $arrays[$arr][$index]; } $counter-&gt;increment(); } print_r($result); </code></pre> <p>I changed $array['A'] $array['B'] to be indexed as $array[0] $array[1] etc to make it easier to work with.</p> <pre><code>counter-&gt;getNum() </code></pre> <p>returns the array indexes. You can choose whether or not you want to actually pick that element.</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