title: Saving arrays with key/value pairs
body: <p>Unable to save arrays with the correct key based on how many inputs the user selects.</p>
  
  <p>I have tried the following:</p>
  
  <p>I've tried to json encode some elements, but to no avail.</p>
  
  <p>Here's the html:</p>
  
  <pre><code>
   &lt;div class="row"&gt;
          &lt;div class="col"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Block Name&lt;/label&gt;
                  &lt;input type="text" name="name_of_blocks[]" class="form-control"&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="col"&gt;
              &lt;label&gt;No of units in the block&lt;/label&gt;
              &lt;input type="number" name="no_units[]" class="form-control" id="blockNo"&gt;
          &lt;/div&gt;
          &lt;div class="col"&gt;
              &lt;label&gt;Number of floors&lt;/label&gt;
              &lt;input type="number" class="form-control" name="no_of_floors[]"&gt;
          &lt;/div&gt;
          &lt;div class="col"&gt;
              &lt;label&gt;Does this block have a lift?&lt;/label&gt;
              &lt;select class="form-control" name="lift[]"&gt;
                  &lt;option&gt;Please Select&lt;/option&gt;
                  &lt;option value="yes"&gt;Yes&lt;/option&gt;
                  &lt;option value="no"&gt;No&lt;/option&gt;
              &lt;/select&gt;
          &lt;/div&gt;
   &lt;/div&gt;
  
   &lt;div class="row"&gt;
          &lt;div class="col"&gt;
              &lt;label&gt;What facilities does this block have?&lt;/label&gt;
              &lt;div class="row"&gt;
                  &lt;div class="col"&gt;
                      &lt;ul class="list-unstyled"&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="fob"&gt; Fob Entry&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="gate_entry"&gt; Gate Entry&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="key"&gt; Key Entry&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="finger"&gt; Fingerprint Entry&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="Code Entry"&gt; Code Entry&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="mailbox"&gt; Mailbox Area&lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/div&gt;
                  &lt;div class="col"&gt;
                      &lt;ul class="list-unstyled"&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="bar"&gt; Bar&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="bike_storage"&gt; Bike Storage&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="car_park"&gt; Secure Car Parking&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="off_road"&gt; Off-road Parking&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="visitor_parking"&gt; Visitor Parking&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="concierge"&gt; Concierge&lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/div&gt;
                  &lt;div class="col"&gt;
                      &lt;ul class="list-unstyled"&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="gym"&gt; Gym&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="pool"&gt; Swimming Pool &lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="snooker"&gt; Snooker/Pool Table&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="cafe"&gt; Cafe&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="maintenance"&gt; Maintenance Service&lt;/li&gt;
                          &lt;li&gt;&lt;input type="checkbox" name="facilities[]" value="various"&gt; Various businesses/commercial outlets&lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/div&gt;
  
  </code></pre>
  
  <p>Heres what I have in my controller, but this either doesn't save correctly or saves one row if there's two and doesn't save the correct info:</p>
  
  <pre><code>$data = $request-&gt;all();
  $count = $data['number_of_blocks'];
  for ($i = 0; $i &lt; $count; $i++) {
  
      $fac = array_values($request-&gt;facilities);
      $facil = json_encode($fac);
  
      $blok = new Blocks;
      $blok-&gt;block_id = $block-&gt;id;
      $blok-&gt;name_of_blocks = array_key_exists($i, $data['name_of_blocks']) ? $data['name_of_blocks'][$i] : null;
      $blok-&gt;no_units = array_key_exists($i, $data['no_units']) ? $data['no_units'][$i] : null;
      $blok-&gt;no_of_floors = array_key_exists($i, $data['no_of_floors']) ? $data['no_of_floors'][$i] : null;
      $blok-&gt;lift = array_key_exists($i, $data['lift']) ? $data['lift'][$i] : null;
      $blok-&gt;facilities = array_key_exists($i, $facil) ? $facil[$i] : null;
      $blok-&gt;save();
  }
  </code></pre>
  
  <p>I do have some clone script to clone the amount of form fields based on how many the user wants, so this could also be a problem, but I'm not sure what or where:</p>
  
  <pre><code>$("#continue").click(function () {
      $("#first").hide();
      $("#second").show();
  
      var blocks = $("#noBlocks").val();
      var name = $("#blockName").val();
      $(".clone").hide();
      var select = parseInt(blocks, 10);
      var $clone = $('.clone').html();
      var html = '';
      while (select &gt; 0) {
          html += $clone;
          select--;
      }
  
      $("#area").empty().html(html);
  
      $("#bno").html(blocks);
      $("#bn").html(name);
  });
  </code></pre>
  
  <p>Basically if the user selects 2 then I want the code to save two rows and the facilities to be json encoded for each row, and match what the user selects. Any help advice greatly appreciated.</p>