dpmopn8542 2018-06-08 03:57
浏览 128
已采纳

在PHP中将$ _Post值存储到数组中

Sorry I don't know how to explain it in title so I just explain it here.

So I have this html and I want to store this in array, this is the code below

<tr>
<td>
<input name="grade_id[]" value="4" type="hidden">
</td>
<td>
<input name="q1_grade[]">94</input>
</td>
<td>
<input name="q2_grade[]">89</input>
</td>
<td>
<input name="q3_grade[]">90</input>
</td>
<td>
<input name="q4_grade[]">88</input>
</td>
</tr>
<tr>
<td>
<input name="grade_id[]" value="7" type="hidden">
</td>
<td>
<input name="q1_grade[]">95</input>
</td>
<td>
<input name="q2_grade[]">77</input>
</td>
<td>
<input name="q3_grade[]">77</input>
</td>
<td>
<input name="q4_grade[]">98</input>
</td>
</tr>

Now from above code I want to manipulate the datas in an array so I put the data specifically in array which the id has its own $q1 $q2 $q3 $q4 to do this is as per code below

<?php
 $grade_idArray = $_POST['grade_id'];
 $q1_gradesArray = $_POST['q1_grade'];
 $q2_gradesArray = $_POST['q2_grade'];
 $q3_gradesArray = $_POST['q3_grade']; 
 $q4_gradesArray = $_POST['q4_grade'];

 $grade = array();
 $gf = array();

 foreach($grade_idArray as $g)
 {
    foreach($q1_gradesArray as $q1)
    {
      foreach($q2_gradesArray as $q2)
      {
        foreach($q3_gradesArray as $q3)
        {
            foreach($q4_gradesArray as $q4)
            {
                $grade = array($g,$q1,$q2,$q3,$q4);
            }
        }
      }
    }
  }

 echo var_dump($grade);
 ?>

But the output data that I am receiving is just one array instead of two arrays. how should i do it?

array(5) { [0]=> string(2) "7" [1]=> string(2) "95" [2]=> string(2) "77" [3]=> string(2) "77" [4]=> string(2) "98" }
  • 写回答

1条回答 默认 最新

  • duanguoyin7008 2018-06-08 04:06
    关注

    You can try this way. May be it would be helpful for you.

    foreach($grade_idArray as $key => $g)
     {
        $grade[$key] = [$g, $q1_gradesArray[$key], $q2_gradesArray[$key], 
        $q3_gradesArray[$key], $q4_gradesArray[$key]]
    }
    echo var_dump($grade);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 编写vba代码实现数据录入工作
  • ¥15 做过TCL海信电视小米电视相关影视会员软件私我
  • ¥15 Mapreduce是正常的,在运行其他jar包时并没有任何问题,只是在做LogCount.jar 时出的问题。如图所示
  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序