Multidimensional html input array create in php, a

2019-08-20 05:04发布

while($eredmeny = mssql_fetch_assoc($result))
{   
$table_results  = '<tr style="color: #000e94; font-size=12" align="center" valign="middle">';
$table_results .= '<td>';
$table_results .= "<input type='checkbox' name='form[][id]' id='form[][id]' value='{$eredmeny['ID']}'>"; 
$table_results .= '</td>';
$table_results .= '<td><input type="text" name="form[][idnap]" id="form[][idnap]" value="' . $eredmeny['ID'] .'" style="width:130px;"></td>';   
$table_results .= '<td><input type="text" name="form[][feladat]" id="form[][feladat]" value="' . $eredmeny['Feladat'] .'" style="width:130px;"></DIV></td>';
$table_results .= '<td><b><input type="text" name="form[][hatarnap]" id="form[][hatarnap]" value="' . date_format(date_create($eredmeny['Hatarido_alap']), 'd') .'" style="width:20px;">nap</b>';
$table_results .= '<input type="text" name="form[][hatarora]" id="form[][hatarora]" value="' . date_format(date_create($eredmeny['Hatarido_alap']), 'H:i:s') . '" style="width:55px;"></td>';
$table_results .= '<td>' . $eredmeny['Tipusa'] . '</td>';
$table_results .= '</tr>';
echo $table_results;
}

I've this code, to create html form. The form is usabe, good. I've got this form in php:

Array
(
    [0] => Array
        (
            [id] => NAPI_01_
        )

    [1] => Array
        (
            [idnap] => NAPI_01_20140220
        )

    [2] => Array
        (
            [feladat] => SM1
        )

    [3] => Array
        (
            [hatarnap] => 01
        )

    [4] => Array
        (
            [hatarora] => 07:15:00

I need an array similar, but I don't know how now.

Array
(
    [0] => Array
        (
            [id] => NAPI_01_
            [idnap] => NAPI_01_20140220
            [feladat] => SM1
            [hatarnap] => 01
            [hatarora] => 07:15:00
        )
[1] => Array
        (
            [id] => NAPI_01_
            [idnap] => NAPI_01_20140220
            [feladat] => sm2
            [hatarnap] => 01
            [hatarora] => 07:15:00
        )

1条回答
你好瞎i
2楼-- · 2019-08-20 05:24

The reason you're getting each form input as a separate element in the main array is because of this: name="form[][idnap]". You create a new item in your main array with [], then add an array key to that item with [idnap].

Try something like this:

$count = 0;

while($eredmeny = mssql_fetch_assoc($result))
{   
$table_results  = '<tr style="color: #000e94; font-size=12" align="center" valign="middle">';
$table_results .= '<td>';
$table_results .= "<input type='checkbox' name='form[$count][id]' id='form[$count][id]' value='{$eredmeny['ID']}'>"; 
$table_results .= '</td>';
$table_results .= '<td><input type="text" name="form[$count][idnap]" id="form[$count][idnap]" value="' . $eredmeny['ID'] .'" style="width:130px;"></td>';   
$table_results .= '<td><input type="text" name="form[$count][feladat]" id="form[$count][feladat]" value="' . $eredmeny['Feladat'] .'" style="width:130px;"></DIV></td>';
$table_results .= '<td><b><input type="text" name="form[$count][hatarnap]" id="form[$count][hatarnap]" value="' . date_format(date_create($eredmeny['Hatarido_alap']), 'd') .'" style="width:20px;">nap</b>';
$table_results .= '<input type="text" name="form[$count][hatarora]" id="form[$count][hatarora]" value="' . date_format(date_create($eredmeny['Hatarido_alap']), 'H:i:s') . '" style="width:55px;"></td>';
$table_results .= '<td>' . $eredmeny['Tipusa'] . '</td>';
$table_results .= '</tr>';
echo $table_results;
$count++;
}
查看更多
登录 后发表回答