简体   繁体   English

HTML表格行作为多维数组

[英]Html Table rows as multidimensional array

I have a table for employee time-sheet. 我有一张员工时间表表。

After filling this time-sheet form I get below array. 填写此时间表表单后,我进入了数组。

Array
(
[date_from] => 2018-01-04
[date_to] => 2018-01-04
[date] => 2018-01-04
[project] => 53
[task] => 1
[time] => 05:30
[date1] => 2018-01-05
[project1] => 54
[task1] => 1
[time1] => 08:00
) 

Now I want this array as 现在我想要这个数组

Array
(
  [date] => Array
 (
    [date_from] => 2018-01-04
    [date_to] => 2018-01-04
 )

[row1] => Array
(
       [date] => 2018-01-04
       [project] => 53
       [task] => 1
       [time] => 05:30
)

[row2] => Array
(
       [date1] => 2018-01-04
       [project1] => 53
       [task1] => 1
       [time1] => 05:30
)

)

some one please help me to this thing sort out. 有人请帮我解决这个问题。

PHP already formats array nicely with arrays by including [] in your form. PHP已经通过在表单中​​包含[]来很好地格式化数组。 You should modify your incoming form like so (This is one example via jQuery, but there are many other ways to add additional rows with an index): 您应该像这样修改传入的表单(这是jQuery的一个示例,但是还有许多其他方法可以通过索引添加其他行):

 var i = 0; function addRow() { i++; $('#form') .append($('<br />')) .append('Date: ') .append($('<input />').attr('name', 'form[' + i + '][date]')) .append('Time: ') .append($('<input />').attr('name', 'form[' + i + '][time]')) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="form"> Date: <input name="form[0][date]" /> Time: <input name="form[0][time]" /> </div> <input type="button" value="Add new" onclick="addRow()" /> 

The reason why I index the rows, is so that $_POST['form'][0] will contain both a date/time, and is segmented. 我为这些行建立索引的原因是,使$_POST['form'][0]既包含日期/时间,又被分段。

foreach ( $_POST['form'] as $dateTimeVals ) {
    //$date = $dateTimeVals['date'];
    //$time = $dateTimeVals['time'];
    //Sweet
}

I could simply omit the index, and use date[] and time[] but then will have to do a for loop to get the data: 我可以简单地省略索引,并使用date[]time[]但随后必须做一个for循环来获取数据:

for ($i=0; $i<count($_POST['date']); $i++) {
    //$date = $_POST['date'][$i];
    //$time = $_POST['time'][$i];
}

Please please please try to use @FrankerZ solution, otherwise you may have to resort to manually testing the field name against a "white list" to determine what makes up a row 请请尝试使用@FrankerZ解决方案,否则,您可能不得不求助于“白名单”手动测试字段名称,以确定组成行的内容

$postData = Array(
    'date_from' => '2018-01-04',
    'date_to' => '2018-01-04',
    'date' => '2018-01-04',
    'project' => '53',
    'task' => '1',
    'time' => '05:30',
    'date1' => '2018-01-05',
    'project1' => '54',
    'task1' => '1',
    'time1' => '08:00'
);

$rows = array();
$rowKeys = array( 'date','project','task','time');
foreach( $postData as $key => $value )
{
    if( preg_match('/\A(\D*)(\d*)\z/', $key, $match ) === 1 and in_array( $match[1], $rowKeys ) === true )
    {
        $rowKey = ( isset( $match[2] ) and empty( $match[2] ) === false ) ? 'row' . ($match[2] + 1) : 'row1';
        if( isset( $rows[ $rowKey ] ) === false )
        {
          $rows[ $rowKey ] = array();
        }
        $rows[ $rowKey ][ $match[1] ] = $value;
        unset( $postData[ $key ] );
    }
}

$rows = array_merge( array( 'date'=> $postData ), $rows );
print_r( $rows );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM