繁体   English   中英

如何获取通过for循环出现的所有元素的名称并将它们放入PHP中的关联数组中

[英]How to get names of all the elements that appear through the for loop and put them into the associative array in PHP

这是我的想法,但不能正常工作。 我尝试将此 [] 括号添加到 HTML 的名称标签中,但我不喜欢我得到的数组。 对不起英语不好

<?php
if (isset($_POST['submit'])) {
    $name=$_POST['name_of_exam'];
    $volume=$_POST['volume'];
    $date=$_POST['date'];
    $attempt=$_POST['attempt'];
    $arr=array(
        array("Name of exam"=>$name, "Volume"=>$volume,"Date"=>$date,"Attempt"=>$attempt)
    );
    print_r ($arr);
    
 }

count 是我在开头输入的数字

for ($i=0; $i < $count; $i++) { ?>
          <div class="input-group">
          <label for="">Name of Exame</label>   
          <input type="text" name="name_of_exame">
         </div>

         <div class="input-group">
          <label for="">Volume</label>   
          <input type="range" min='0' max='10' value='0' name="volume">
         </div>

         <div class="input-group">
          <label for="">Date</label>   
          <input type="date" name="date">
         </div>

         <div class="input-group">
          <label for="">Attempt</label>   
          <input type="text" name="attempt">
         </div>

        <?php 
     
        }   ?>
         <input type="submit" name="submit" id="">

我期望结果如下:

$arr=array(
array('Name'=>'Math','Volume'=>10,'Date'=>'1/30/2022','Attempt'=>2),
array('Name'=>'History','Volume'=>7,'Date'=>'1/17/2022','Attempt'=>1),
array('Name'=>'Geography','Volume'=>3,'Date'=>'1/10/2022','Attempt'=>4)
...
);

您需要特定的字段名称,例如exams[0][name_of_exam] 使用循环中的$i

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM