title: Change my array so values are stored together
body: <p>I have multiple possible users that are posted from a form to my PHP script through AJAX. </p>
  
  <p>In that script I have the following code:</p>
  
  <pre><code>parse_str($_POST['users'], $useroutput);
  
  foreach($useroutput as $key =&gt; $user){
    // If both fields are filled in:
    if(!empty($user['username']) OR !empty($user['password'])){
      $userstring .= $user['username'].' '.$user['password'].'&lt;br&gt;';
    }else{
      echo 'Empty';
    }
  }
  </code></pre>
  
  <p>However the above loop always shows me 'Empty'.</p>
  
  <p>While this is what my array looks like if I print it:</p>
  
  <pre><code>[username] =&gt; Array
          (
              [0] =&gt; username
              [1] =&gt; anotherusername
          )
  
      [password] =&gt; Array
          (
              [0] =&gt; password
              [1] =&gt; anotherpassword
          )
  
  )
  </code></pre>
  
  <p>How can I change my array to look like this?:</p>
  
  <pre><code>Array(
      [0] =&gt; Array
      (
          [username] = myusername
          [password] = mypassword
      )
      [1] =&gt; Array
      (
          [username] = anotherusername
          [password] = anotherpassword
      )
  )
  </code></pre>
  
  <p>I've tried different ways of posting the data but until now nothing gives me the array as I need it.</p>
  
  <p>This is how I post my form data to my PHP script:</p>
  
  <pre><code>// Add/edit users script
    $( "#companywrap" ).on("click", "#saveuser", function( event ) {
      // Stop normal form behaviour
      event.preventDefault();
      var $form = $("#userform"),
      postBody = $form.serialize(),
      url = $form.attr( "action" );
  
      var posting = $.post( url, {users: postBody});
      // Show result in a div
      posting.done(function( data ) {
        $( ".resultmessageuser" ).empty().slideDown('fast').append( data );
      });
    });
  </code></pre>
  
  <p>My form markup as requested:</p>
  
  <pre><code>&lt;form id="userform" action="includes/userscript.php" method="post" enctype="multipart/form-data"&gt;
    &lt;div class="card m-b-20"&gt;
      &lt;div class="card-body"&gt;
        &lt;div class="form-group fieldGroup"&gt;
  
              &lt;div class="form-group row"&gt;
                  &lt;label for="example-text-input" class="col-sm-4 col-form-label"&gt;Gebruikersnaam&lt;/label&gt;
                  &lt;div class="col-sm-8"&gt;
                      &lt;input class="form-control" name="username[]" value="&lt;?PHP echo $getcompany['username']; ?&gt;" type="text" required&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group row"&gt;
                  &lt;label for="example-text-input" class="col-sm-4 col-form-label"&gt;Wachtwoord&lt;/label&gt;
                  &lt;div class="col-sm-8"&gt;
                      &lt;input class="form-control" name="password[]" placeholder="&lt;?PHP echo $editpass; ?&gt;" value='' type="text" required&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
                &lt;div class="input-group-addon"&gt;
                    &lt;a href="javascript:void(0)" class="btn btn-success addMore"&gt;&lt;span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"&gt;&lt;/span&gt; Extra gebruiker&lt;/a&gt;
                &lt;/div&gt;
  
        &lt;/div&gt;
  
        &lt;!-- copy of input fields group --&gt;
        &lt;div class="form-group fieldGroupCopy" style="display: none;"&gt;
  
              &lt;div class="form-group row"&gt;
                  &lt;label for="example-text-input" class="col-sm-4 col-form-label"&gt;Gebruikersnaam&lt;/label&gt;
                  &lt;div class="col-sm-8"&gt;
                      &lt;input class="form-control" name="username[]" value="" type="text" id="example-text-input"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group row"&gt;
                  &lt;label for="example-text-input" class="col-sm-4 col-form-label"&gt;Wachtwoord&lt;/label&gt;
                  &lt;div class="col-sm-8"&gt;
                      &lt;input class="form-control" name="password[]" placeholder="&lt;?PHP echo $editpass; ?&gt;" value='' type="text" id="example-text-input"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
                &lt;div class="input-group-addon"&gt;
                    &lt;a href="javascript:void(0)" class="btn btn-danger remove"&gt;&lt;span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"&gt;&lt;/span&gt; Verwijder velden&lt;/a&gt;
                &lt;/div&gt;
  
        &lt;/div&gt;
  
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>