title: Loop through arrays with limitations
body: <p>I have an array of transports:</p>
  <pre><code>$transports = [
    0 =&gt; ['label' =&gt; 'Transport 1', 'maxPalettesToLoad' =&gt; 5],
    1 =&gt; ['label' =&gt; 'Transport 2', 'maxPalettesToLoad' =&gt; 2]
  ];
  </code></pre>
  <p>and an array of palettes with certain products i want to &quot;load&quot; on the transport above</p>
  <pre><code>$palettes = [
    0 =&gt; ['id_product' =&gt; 2, 'id_sale_order_item' =&gt; 1, 'amount' =&gt; 4],
    1 =&gt; ['id_product' =&gt; 4, 'id_sale_order_item' =&gt; 4, 'amount' =&gt; 3]
  ];
  </code></pre>
  <p>What I'm struggling to accomplish is to load all the palettes onto transports where total number of loaded palettes is equal to <code>maxPalettesToLoad</code> parameter of each transport. I'm looking for a solution that will generate me values like:</p>
  <pre><code>$toret = [
    'Transport 1' =&gt; [
         0 =&gt; ['id_product' =&gt; 2, 'id_sale_order_item' =&gt; 1],
         1 =&gt; ['id_product' =&gt; 2, 'id_sale_order_item' =&gt; 1],
         2 =&gt; ['id_product' =&gt; 2, 'id_sale_order_item' =&gt; 1],
         3 =&gt; ['id_product' =&gt; 2, 'id_sale_order_item' =&gt; 1],
         4 =&gt; ['id_product' =&gt; 4, 'id_sale_order_item' =&gt; 4]
    ],
    'Transport 2' =&gt; [
         0 =&gt; ['id_product' =&gt; 4, 'id_sale_order_item' =&gt; 4],
         1 =&gt; ['id_product' =&gt; 4, 'id_sale_order_item' =&gt; 4]
    ],
  ];
  </code></pre>
  <p>EDIT: Here is my attempt. It's kinda different in terms of data structure, because I simplified it to make it shorted</p>
  <pre><code>                foreach ($transportsData as $transportData) {
  
                      $modelTransportOrder = new TransportOrder();
                      $modelTransportOrder-&gt;max_palettes_amount = $maxPalettesAmount;
                      $status &amp;= $modelTransportOrder-&gt;save();
  
                      $amountAssigned = $transportData['palletes'];
  
                      if ($status) {
                          $j = 1;
  
                          foreach ($palettesData as $paletteData) {
                              if ($modelSaleOrderItem = SaleOrderItem::findOne([$paletteData['id_sale_order_item']])) {
  
                                  for ($i = 0; $i &lt; $paletteData['amount']; $i++) {
  
                                      if ($j &lt;= $amountAssigned) {
                                          $modelTransportOrderItem = new TransportOrderItem();
                                          $modelTransportOrderItem-&gt;id_transport_order = $modelTransportOrder-&gt;id;
                                          $modelTransportOrderItem-&gt;id_product = $modelSaleOrderItem-&gt;id_product;
                                          $modelTransportOrderItem-&gt;id_sale_order_item = $modelSaleOrderItem-&gt;id;
                                          $modelTransportOrderItem-&gt;index = $j;
                                          $status &amp;= $modelTransportOrderItem-&gt;save();
  
                                          // Index reset
                                          if ($j == $amountAssigned) {
                                              $j = 0;
                                              break;
                                          }
                                      }
  
                                      $j++;
                                  }
  
                              } else {
                                  $status = false;
                              }
  
                          }
                      } else {
                          // Zapis transportu nie powiódł się
                      }
                  }
  </code></pre>