首页 > 解决方案 > 如何将数组的数字索引转换为关联数组

问题描述

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

所以,这就是 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.phpprint_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循环将返回所有名字。

标签: phphtmlarrays

解决方案


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

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变量的,因为 this:$$i++看起来很奇怪并且可能会导致问题。我会$i = 0在循环之前设置,然后将 a$i++作为循环关闭之前的最后一项}


推荐阅读