Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe you can use this function, assuming that:</p> <p>... <code>$inventory</code> is already sorted (all <code>A</code> come before <code>B</code>)<br /> ... <code>$inventory</code> is a numeric array staring at index zero </p> <pre><code>// $set is the collection to which the generated sets are appended // $inventory is your inventory, see the assumptions above // $aCount - the number of A elements in a set // $bCount - the number of B elements in a set function makeSets(array &amp;$sets, array $inventory, $aCount, $bCount) { // extract $aItems from $inventory and shorten $inventory by $aCount $aItems = array_splice($inventory, 0, $aCount); $bItems = array(); // iterate over $inventory until a B item is found foreach($inventory as $index =&gt; $item) { if($item[0] == 'B') { // extract $bItems from $inventory and shorten $inventory by $bCount // break out of foreach loop after that $bItems = array_splice($inventory, $index, $bCount); break; } } // append $aItems and $bItems to $sets, padd this array with null if // less then $aCount + $bCount added $sets[] = array_pad(array_merge($aItems, $bItems), $aCount + $bCount, null); // if there are still values left in $inventory, call 'makeSets' again if(count($inventory) &gt; 0) makeSets($sets, $inventory, $aCount, $bCount); } $sets = array(); makeSets($sets, $inventory, 1, 2); print_r($sets); </code></pre> <p>Since you mentioned that you dont have that much experience with arrays, here are the links to the php documentation for the functions I used in the above code:</p> <ul> <li><a href="http://de2.php.net/manual/en/function.array-splice.php" rel="nofollow noreferrer"><code>array_splice</code></a> — Remove a portion of the array and replace it with something else</li> <li><a href="http://de2.php.net/manual/en/function.array-merge.php" rel="nofollow noreferrer"><code>array_merge</code></a> — Merge one or more arrays</li> <li><a href="http://de2.php.net/manual/en/function.array-pad.php" rel="nofollow noreferrer"><code>array_pad</code></a> — Pad array to the specified length with a value</li> </ul>
    singulars
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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