title: Need an efficient way to break down a quantity into specified order sizes
body: <p>So I need a method that will break down an order into the specified sizes. For example:</p>
  
  <p><code>orderBreakdown(40, [4, 8, 12, 16]);</code>
  Would return <code>[16, 16, 8]</code> (an array of the orders). It needs to have the most efficient breakdown (can't be 12, 12, 12, 4). It also has to be extremely flexible - it can fail (obviously some quantities can't be broken down, for example 39 in that case). </p>
  
  <p>Right now I have:</p>
  
  <pre><code>function quantityBreakdown(int $count, array $units)
  {
      sort($units);
  
      $units = array_reverse($units); //order units from large to small
  
      $orders = [];
  
      $original_count = $count;
  
      while ($count &gt; 0 &amp;&amp; !empty($units))
      {
  
          for($i = 0; $i &lt; count($units); $i++)
          {
  
              $unit = $units[$i];
  
              while($count &gt;= $unit){
                  array_push($orders, $unit);
                  $count = $count - $unit;
              }
          }
  
          if ($count &gt; 0){
              $count = $original_count;
              $orders = [];
              array_shift($units);
          }
  
      }
  
      return $orders;
  
  }
  </code></pre>
  
  <p>This works in a lot of cases:
  <code>quantityBreakdown(19, [6, 8, 11, 17]);</code> will return <code>[11, 8]</code> for example. But there are some cases when it will return non-optimal results. <code>quantityBreakdown(36, [6, 8, 11, 17])</code> will return <code>[6, 6, 6, 6, 6, 6]</code>, when optimally it should return <code>[17, 11, 8]</code>. I'm a bit at a loss of where to go from here.</p>