如何从以下表单中读取POST数据,由于某些原因我没有使用像Customer [0] [fullname]这样的索引,而是使用Customer [] [fullname]
<form action="form.php" method="post">
<input type="text" name="Customer[][fullname]" />
<input type="text" name="Customer[][nic]" />
<input type="text" name="Customer[][fullname]" />
<input type="text" name="Customer[][nic]" />
<input type="text" name="Customer[][fullname]" />
<input type="text" name="Customer[][nic]" />
<input type="submit" />
</form>
这是我的数组
的var_dumpArray (
[Customer] => Array (
[0] => Array (
[fullname] => abc
)
[1] => Array (
[fullname] => def
)
[2] => Array (
[fullname] => xyz
)
[3] => Array (
[nic] => 111
)
[4] => Array (
[nic] => 222
)
[5] => Array (
[nic] => 333
)
)
)
我想在数组下面打破数组
array("fullname"=>abc,"nic"=>111)
array("fullname"=>def,"nic"=>222)
array("fullname"=>xyz,"nic"=>333)
答案 0 :(得分:1)
$half = count($array['Customer'])/2;
for($i = 0; $i < $half; $i++) {
$result[$i] = $array['Customer'][$i];
}
for($i = $half; $i < $half * 2; $i++) {
$result[$i - $half] = array_merge($result[$i - $half], $array['Customer'][$i]);
}
$result
的每个值都是您想要的。
[0] => array('fullname'=>'abc','nic'=>111)
[1] => array('fullname'=>'def','nic'=>222)
[2] => array('fullname'=>'xyz','nic'=>333)
答案 1 :(得分:1)
以下是我提出的内容,它适用于评论中要求的两个以上字段。
<强>代码:强>
<?php
$array = array(
array('fullname' => 'abc'),
array('fullname' => 'def'),
array('fullname' => 'ghi'),
array('nic' => '111'),
array('nic' => '222'),
array('nic' => '333')
);
$index = 0;
foreach($array as $value) {
if (!isset($old)) {
$old = key($value);
}
if (key($value) != $old) {
$index = 0;
$old = key($value);
}
$result[$index][key($value)] = $value[key($value)];
$index++;
}
print_r($result);
<强>输出:强>
Array
(
[0] => Array
(
[fullname] => abc
[nic] => 111
)
[1] => Array
(
[fullname] => def
[nic] => 222
)
[2] => Array
(
[fullname] => ghi
[nic] => 333
)
)
<强> LIVE DEMO 强>