HTML form passing two PHP markup arrays (special case I think)

This html code

<input name="Html_Array[][title]">
<input name="Html_Array[][amount]">

Generate this PHP array

    [0] => Array
            (
                [title] => Seilpendel fΓΌr Tragsysteme
            )

        [1] => Array
            (
                [amount] => 2
            )...So on

What will be the HTML to create the next PHP array

        [0] => Array
            (
                [title] => Seilpendel fΓΌr Tragsysteme
                [amount] => 1
            )

        [1] => Array
            (
                [title] => Article Tiel
                [amount] => 2
            )

The size of the array is unknown, so we cannot index the hard code index 0,1,2, etc.

+3
source share
4 answers

Keep index every time you add a new group of form fields (uses jQuery):

window.count = 0;
$('#add-more-button').on('click', function() {
 $('<input name="html_array[' + window.count + '][title] />').appendTo('form:first');
 $('<input name="html_array[' + window.count + '][amount] />').appendTo('form:first');
 window.count++;
});

You should get the right structure.

Edit if you mean PHP:

// Assuming 0-indexed array
foreach($my_array as $key => $value) {
 echo '<input name="html_array[' . $key . '][title]" />';
 echo '<input name="html_array[' . $key . '][value]" />';
}
+2
source

I do not think you can create this exact structure in HTML. Just merge them:

$result = array();
$current = array();

foreach($input as $item) {
    $k = reset(keys($item));

    if(isset($current[$k])) {
        $result[] = $current;
        $current = array();
    }

    $current[$item] = $item[$k];
}
+1
source
 <input name="Html_Array[0][title]">
 <input name="Html_Array[0][amount]">

 <input name="Html_Array[1][title]">
 <input name="Html_Array[1][amount]">

..

.

+1

,

 <input name="Html_Array[][title]">
 <input name="Html_Array[][amount]">
0

All Articles