在php中提取数字索引数组

在php中提取数字索引数组,php,arrays,Php,Arrays,我有几个Dynamicly build输入字段,其形式如下: <input type="text" class="form-control" name="person[]" placeholder="Person" /> <input type="text" class="form-control" name="function[]" placeholder="Function" /> <input type="text" class="form-control"

我有几个Dynamicly build输入字段,其形式如下:

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />
foreach($_POST['person'] as $key => $value) {
    echo 'Person ' . $value . ' has function ' . $_POST['function'][$key] . '<br>';
}

这为我提供了
$\u POST
中特定数组
person[]
的列表,但这对我没有用处

听起来“人”的数量和“功能”的数量是一样的。如果是这种情况,那么您可以这样做:

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />
foreach($_POST['person'] as $key => $value) {
    echo 'Person ' . $value . ' has function ' . $_POST['function'][$key] . '<br>';
}
foreach($\u POST['person']作为$key=>$value){
回显“Person”。$value.“具有函数”。$\u POST['function'][$key]。
”; }
类似的方法可能会奏效

$sql = "INSERT INTO tbl (Person,Function) VALUES ";
$components = array();

$totalitemcount = count($_POST['person']);

for ($i=0;$i<=$totalitemcount;$i++) {
    $components[] = "('".$_POST['person'][$i]."','".$_POST['function'][$i]."')";
}

$insertstring = implode(",",$components);

$finalsql = $sql.$insertstring;

//do insert here
$sql=“插入tbl(人员、职能)值”;
$components=array();
$totalitemcount=计数($_POST['person']);

对于($i=0;$i),如果您没有办法确保人员和功能输入以某种方式联系在一起,则此表单设计将导致问题。假设有人在填写表单,他们为一个人保留了空白功能:

Person    Function

Anna      array_map
Betty
Claire    count
          date_format
表单逻辑将把
Betty
count
Claire
date\u格式配对,而不是知道
Betty
的函数是空的。因此,最好用一个ID对每个人/函数组合进行分组,这样你就可以区分它们了。在这两种情况下使用迭代器都很容易JS或PHP

新表格:

<input type="text" class="form-control" name="persfunc[1][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[1][f]" placeholder="Function" />

<input type="text" class="form-control" name="persfunc[2][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[2][f]" placeholder="Function" />

<input type="text" class="form-control" name="persfunc[3][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[3][f]" placeholder="Function" />

<input type="text" class="form-control" name="persfunc[4][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[4][f]" placeholder="Function" />

这是一种处理表单数据的更为健壮的方法,而不仅仅是相信输入会匹配,而且不需要更多的编码。

您是否尝试过使用php内置的
count
函数?只需使用count函数,或使用foreach循环……这些输入是动态的,这意味着有一个按钮
+add more
volves javascript?@Ghost这是一个+添加更多我用jQuery@Peter如果您使用jQuery,我将为每个人员/功能对创建新ID。在这种情况下,您可以确定ID和用户是成对的。您可以添加:
if(count($\u POST['person'])==count($\u POST['function'])){…
您应该注意,数据绝对不会逃逸,而且很容易被sql注入。您忘了提到这一点:)用户应该自己做,或者最好使用pdo/mysqli prepare等。我会尝试一下,看看什么是最好的,然后回来!@Sharky我熟悉pdo和prepared语句&binds;)所以这不是问题所在。无论如何,谢谢!
Person Anna has function array_map

Missing full data for person_2

Person Claire has function count

Missing full data for person_person_4