title: Algorithm to walk through multiple array values to fill buckets in another array
body: <p>I can't think of a better way to phrase it, but here's what I want to do.  I'm working on a package optimizer class for a shopping cart.  The idea is that up to X widgets of varying type can fit in a single container.  The catch is the widgets are not all the same size, so if say we have items A (largest), B, and C (smallest) we must use the A-sized X-item container.</p>
  
  <p>Here's the dimensions array for my widgets</p>
  
  <pre><code>$widgets = array('WidgetA' =&gt; 
      array (
        'length' =&gt; 10,
        'width' =&gt; 10,
        'height' =&gt; 10,
        'weight' =&gt; 10
      ),
      'WidgetB' =&gt; 
      array (
        'length' =&gt; 9,
        'width' =&gt; 9,
        'height' =&gt; 9,
        'weight' =&gt; 9
      ),
      'WidgetC' =&gt; 
      array (
        'length' =&gt; 8,
        'width' =&gt; 8,
        'height' =&gt; 8,
        'weight' =&gt; 8
      )
  );
  </code></pre>
  
  <p>Here's the array for the packaging options for these widgets, the key is the number of widgets that are supported by the related dimensions if it were all of the given widget type.</p>
  
  <pre><code>$packaging_options = array(
  6 =&gt; 
  array (
    'A' =&gt; 
    array (
      'length' =&gt; 100,
      'width' =&gt; 100,
      'height' =&gt; 100,
      'weight' =&gt; 100,
    ),
    'B' =&gt; 
    array (
      'length' =&gt; 90,
      'width' =&gt; 90,
      'height' =&gt; 90,
      'weight' =&gt; 90
    ),
    'C' =&gt; 
    array (
      'length' =&gt; 80,
      'width' =&gt; 80,
      'height' =&gt; 80,
      'weight' =&gt; 80
    )
  ),
  5 =&gt; 
  array (
    'A' =&gt; 
    array (
      'length' =&gt; 95,
      'width' =&gt; 95,
      'height' =&gt; 95,
      'weight' =&gt; 95,
    ),
    'B' =&gt; 
    array (
      'length' =&gt; 85,
      'width' =&gt; 85,
      'height' =&gt; 85,
      'weight' =&gt; 85
    ),
    'C' =&gt; 
    array (
      'length' =&gt; 75,
      'width' =&gt; 75,
      'height' =&gt; 75,
      'weight' =&gt; 75
    )
  ),
  2 =&gt; 
  array (
    'A' =&gt; 
    array (
      'length' =&gt; 20,
      'width' =&gt; 20,
      'height' =&gt; 20,
      'weight' =&gt; 20,
    ),
    'B' =&gt; 
    array (
      'length' =&gt; 18,
      'width' =&gt; 18,
      'height' =&gt; 18,
      'weight' =&gt; 18
    ),
    'C' =&gt; 
    array (
      'length' =&gt; 80,
      'width' =&gt; 80,
      'height' =&gt; 80,
      'weight' =&gt; 80
    )
  ),
  1 =&gt; 
  array (
    'A' =&gt; 
    array (
      'length' =&gt; 10,
      'width' =&gt; 10,
      'height' =&gt; 10,
      'weight' =&gt; 10,
    ),
    'B' =&gt; 
    array (
      'length' =&gt; 9,
      'width' =&gt; 9,
      'height' =&gt; 9,
      'weight' =&gt; 9
    ),
    'C' =&gt; 
    array (
      'length' =&gt; 8,
      'width' =&gt; 8,
      'height' =&gt; 8,
      'weight' =&gt; 8
    )
  );
  </code></pre>
  
  <p>And here's a sample array of products from the cart.  Key is the widget id and the value is the quantity.</p>
  
  <pre><code>$products = array(
     'A' =&gt; 14,
     'B' =&gt; 8,
     'C' =&gt; 23
  );
  </code></pre>
  
  <p>I ultimately am trying to end up with an array of xpackages composed of entries that look like:</p>
  
  <pre><code>array(
     'length' =&gt; x,
     'height' =&gt; x,
     'width' =&gt; x,
     'weight' =&gt; (sum of the individual box weights allocated to this package)
  );
  </code></pre>
  
  <p>I just can't figure out a good way to look through that products array to allocate to the boxes.  It seems fairly simple when I just look at it, but  the coding logic is just getting out of control and I know there has to be a better way than how I'm approaching it.  I'm thinking recursion, but the solution is just not clicking for me.</p>