我已经创建了如下动态复选框
$extList = array(
array("server" => "whois.verisign-grs.net", "extName" => "com"),
array("server" => "whois.verisign-grs.net", "extName" => "com.tr"),
array("server" => "whois.verisign-grs.net", "extName" => "tv"),
array("server" => "whois.verisign-grs.net", "extName" => "net"),
array("server" => "whois.verisign-grs.net", "extName" => "gen.tr"),
array("server" => "whois.verisign-grs.net", "extName" => "de"),
array("server" => "whois.verisign-grs.net", "extName" => "org"),
);
foreach ($extList as $key => $value) {
?>
<span> <input type="checkbox" name="ext[]"
value="<?= $value["extName"] ?>"> <?= $value["extName"]
我想要做的是将theese值添加到数组中,我已经尝试过类似的操作,但是我想我错过了一些事情,因为它只显示一条记录
for ($i = 0; $i < count($_POST["ext"]); $i++) {
$list = array(
"extName" => $_POST["ext"][$i],
"server" => $extList[$i]["server"]
);
array_push($extAndServer, $list);
}
答案 0 :(得分:1)
您只需要在使用array_push()
之前声明数组,如下所示:
$extAndServer = array(); // declare your array
我尝试了这个示例:
您的数组:
<?php
$extList = array(
array("server" => "whois.verisign-grs.net", "extName" => "com"),
array("server" => "whois.verisign-grs.net", "extName" => "com.tr"),
array("server" => "whois.verisign-grs.net", "extName" => "tv"),
array("server" => "whois.verisign-grs.net", "extName" => "net"),
array("server" => "whois.verisign-grs.net", "extName" => "gen.tr"),
array("server" => "whois.verisign-grs.net", "extName" => "de"),
array("server" => "whois.verisign-grs.net", "extName" => "org"),
);
?>
表格:
<form method="post">
<?php
foreach ($extList as $key => $value) {
?>
<span> <input type="checkbox" name="ext[]"
value="<?= $value["extName"] ?>"> <?= $value["extName"] ?>
<?php
}
?>
<input type="submit" name="submit">
</form>
结果:
<?php
if(count($_POST) > 0){
$extAndServer = array(); // this line will solve your problem
for ($i = 0; $i < count($_POST["ext"]); $i++) {
$list = array(
"extName" => $_POST["ext"][$i],
"server" => $extList[$i]["server"]
);
array_push($extAndServer, $list);
}
print_r($extAndServer);
}
?>