[php] Html/PHP - Form - Input as array

I got a form like this

<form>
<input type="text" class="form-control" placeholder="Titel" name="levels[level]">
<input type="text" class="form-control" placeholder="Titel" name="levels[build_time]">

<input type="text" class="form-control" placeholder="Titel" name="levels[level]">
<input type="text" class="form-control" placeholder="Titel" name="levels[build_time]">

</form>

What I'd like to have as $_POST output is an array like

Array ( 
  [1] => Array ( [level] => 1 [build_time] => 123 ) 
  [2] => Array ( [level] => 2 [build_time] => 456 )
)

I know I could do something like name="levels[1][build_time]" and so on but since these elements get added dynamically, it would be hard to add an index. Is there any other way?

EDIT:

As suggested, I changed my form. I also included my whole HTML now, because I think I'm missing something here. My HTML now:

<div class="form-group">
  <label class="col-md-2">Name(z.B. 1)</label>
  <div class="col-md-10">
    <input type="text" class="form-control" placeholder="Titel" name="levels[][level]">
  </div>

  <label class="col-md-2">Bauzeit(In Sekunden)</label>
  <div class="col-md-10">
    <input type="text" class="form-control" placeholder="Titel" name="levels[][build_time]">
  </div>
</div>

<div class="form-group">
  <label class="col-md-2">Name(z.B. 1)</label>
  <div class="col-md-10">
    <input type="text" class="form-control" placeholder="Titel" name="levels[][level]">
  </div>

  <label class="col-md-2">Bauzeit(In Sekunden)</label>
  <div class="col-md-10">
    <input type="text" class="form-control" placeholder="Titel" name="levels[][build_time]">
  </div>
</div>

The output I get now is:

[levels] => Array ( 
  [0] => Array ( [level] => 1 ) 
  [1] => Array ( [build_time] => 234 ) 
  [2] => Array ( [level] => 2 ) 
  [3] => Array ( [build_time] => 456 ) 
)

Edit 2:

As suggested in your edit, I edited my form and moved the square brackets to the end of the name. The output I get now is:

[levels] => Array ( 
  [level] => Array ( 
    [0] => 1 
    [1] => 2 
  ) 
  [build_time] => Array ( 
    [0] => 234 
    [1] => 456 
  )
) 

I guess that would kinda work but it still looks complicated. No better way?

This question is related to php html html-input

The answer is


in addition: for those who have a empty POST variable, don't use this:

name="[levels][level][]"

rather use this (as it is already here in this example):

name="levels[level][]"

HTML: Use names as

<input name="levels[level][]">
<input name="levels[build_time][]">

PHP:

$array = filter_input_array(INPUT_POST);
$newArray = array();
foreach (array_keys($array) as $fieldKey) {
    foreach ($array[$fieldKey] as $key=>$value) {
        $newArray[$key][$fieldKey] = $value;
    }
}  

$newArray will hold data as you want

Array ( 
  [0] => Array ( [level] => 1 [build_time] => 123 ) 
  [1] => Array ( [level] => 2 [build_time] => 456 )
)

If is ok for you to index the array you can do this:

<form>
    <input type="text" class="form-control" placeholder="Titel" name="levels[0][level]">
    <input type="text" class="form-control" placeholder="Titel" name="levels[0][build_time]">

    <input type="text" class="form-control" placeholder="Titel" name="levels[1][level]">
    <input type="text" class="form-control" placeholder="Titel" name="levels[1][build_time]">

    <input type="text" class="form-control" placeholder="Titel" name="levels[2][level]">
    <input type="text" class="form-control" placeholder="Titel" name="levels[2][build_time]">
</form>

... to achieve that:

[levels] => Array ( 
  [0] => Array ( 
    [level] => 1 
    [build_time] => 2 
  ) 
  [1] => Array ( 
    [level] => 234 
   [build_time] => 456 
  )
  [2] => Array ( 
    [level] => 111
    [build_time] => 222 
  )
) 

But if you remove one pair of inputs (dynamically, I suppose) from the middle of the form then you'll get holes in your array, unless you update the input names...


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to html-input

Using Pipes within ngModel on INPUT Elements in Angular HTML 5 input type="number" element for floating point numbers on Chrome Html/PHP - Form - Input as array How to locate and insert a value in a text box (input) using Python Selenium? Drop Down Menu/Text Field in one HTML5 pattern for formatting input box to take date mm/dd/yyyy? How to add button inside input How to handle floats and decimal separators with html5 input type number How to set default value to the input[type="date"] Get data from file input in JQuery