【问题标题】:How to convert array's numeral index into associative array如何将数组数字索引转换为关联数组
【发布时间】:2020-06-18 06:12:28
【问题描述】:

目前我正在努力发布动态表单。我正在使用简单的帖子,但是如果有任何方法可以从整个表中获取数据,我想知道。

那么,这就是 HTML 表格:

    <form id="importForm" method="post" action="/modules/import/test.php">
    <table class="table table-bordered table-hover table-striped table-sm">
        <thead class="bg-dark thwhite thead-dark">
            <td><small>First name</small></td>
            <td><small>Last name</small></td>
            <td><small>Start date</small></td>
            <td><small>Location</small></td>
            <td><small>Job title</small></td>
            <td><small>Department</small></td>
            <td><small>Manager</small></td>
            <td><small>Hardware</small></td>
            <td><small>Remarks</small></td>

        </thead>

    <?php
    $i = 1;
    foreach($values as $row){ 

        <tr>
        <td style="display:none">
        <input type="hidden" name="id[]" value="<?php echo $$i++ ?>">
        </td>
        <td>
        <small><?php echo $row[0] ?></small>
        <input type="hidden" name="firstname[]" value="<?php echo $row[0] ?>">
        </td>
        <td>
        <small><?php echo $row[1] ?></small>
        <input type="hidden" name="lastname[]" value="<?php echo $row[1] ?>">
        </td>
        <td>
        <small><?php echo $row[3] ?></small>
        <input type="hidden" name="startdate[]" value="<?php echo $row[3] ?>">
        </td>
        <td>
        <small><?php echo $row[2] ?></small>
        <input type="hidden" name="location[]" value="<?php echo $row[2] ?>">
        </td>
        <td>
        <small><?php echo $row[4] ?></small>
        <input type="hidden" name="jobtitle[]" value="<?php echo $row[4] ?>">
        </td>
        <td>
        <small><?php echo $row[5] ?></small>
        <input type="hidden" name="department[]" value="<?php echo $row[5] ?>">
        </td>
        <td>
        <small><?php echo $row[6] ?></small>
        <input type="hidden" name="manager[]" value="<?php echo $row[6] ?>">
        </td>
        <td>
        <small><?php echo $row[11] ?></small>
        <input type="hidden" name="hardware[]" value="<?php echo $row[11] ?>">
        </td>
        <td>
        <small><?php echo $row[15] ?></small>
        <input type="hidden" name="remarks[]" value="<?php echo $row[15] ?>">
        </td>
    </tr>
    <?php }

} ?>
</table>
<div class="w100 tleft">
<button id="importList" class="inline">Import</button>
</div>
</form>

test.php 文件中的print_r($_POST) 如下所示:

Array ( 
    [id] => Array ( 
        [0] => 
        [1] => 1 
        [2] => 2 
        [3] => 3 
    ) 
    [firstname] => Array ( 
        [0] => Michael 
        [1] => Dwight 
        [2] => Jim 
        [3] => Andrew 
    ) 
    [lastname] => Array ( 
        [0] => Scott 
        [1] => Schrute 
        [2] => Halpert 
        [3] => Bernard 
    ) 
    [startdate] => Array ( 
        [0] => 1-7-2020 
        [1] => 1-7-2020 
        [2] => 1-7-2020 
        [3] => 1-7-2020 
    ) 
    [location] => Array ( 
        [0] => Scranton office 
        [1] => Scranton office 
        [2] => Scranton office 
        [3] => Scranton office 
    ) 
    [jobtitle] => Array ( 
        [0] => Regional manager 
        [1] => Assistant to the regional manager 
        [2] => Salesman 
        [3] => Salesman 
    ) 
    [department] => Array ( 
        [0] => Management 
        [1] => Sales 
        [2] => Sales 
        [3] => Sales 
    ) 
    [manager] => Array ( 
        [0] => David Wallace 
        [1] => Michael Scott 
        [2] => Michael Scott
        [3] => Michael Scott 
    ) 
    [hardware] => Array ( 
        [0] => Laptop, laptop bag, desk phone 
        [1] => None 
        [2] => None 
        [3] => None 
    ) 
    [remarks] => Array ( 
        [0] => 
        [1] => 
        [2] => 
        [3] => 
    ) 
)

foreach ($_POST as $row){ echo $row; } 的输出不工作,我需要回显$row[1],然后我有整行,但我无法控制像firstname 或其他字段。

我想将其转换为有可能这样做 echo $row['firstname'] 然后foreach 循环将返回所有名字。

【问题讨论】:

  • 我认为你不需要转换任何东西?您有以下值: foreach ($_POST['firstname'] as $firstname){ echo $firstname; } ?

标签: php html arrays


【解决方案1】:

将输入的名称更改为以下格式:

name="person[$i][firstname]"
name="person[$i][lastname]"
...

$_POST 将如下所示:

Array(
    [person] => Array( 
        [0] => Array(
            [firstname] => "Foo"
            [lastname]  => "Bar"
            ...
        )
        [1] => Array(
            [firstname] => "Foo2"
            [lastname]  => "Bar2"
            ...
        )
        ...
)

您现在可以像这样循环遍历这些值:

foreach ($_POST['person'] as $row) {
    echo $row['firstname'];
    echo $row['lastname'];
    ...
}

注意:不过,我不确定您是如何使用 $i 变量的,因为:$$i++ 看起来很奇怪,可能会导致问题。我会在循环之前设置$i = 0,然后在循环结束} 之前将$i++ 作为最后一项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    相关资源
    最近更新 更多