php中的三维与单维数组

时间:2015-03-09 10:18:12

标签: php html multidimensional-array

对于我的一个项目,由个别学生设计得分。有4个班级(第7班,第8班和第10班),每班有大约25名学生(他们的Roll_No是:1001,1002,1003,1004 ......)得分的标记是连续的:物理,化学,数学和英语。

以上标记的三维数组:

$marks = array(
    "7" => array(
        "1001" => array(90, 95, 98, 78),
        "1002" => array(91, 90, 93, 77),
        "1003" => array(88, 83, 90, 68),
        "1004" => array(90, 91, 90, 80)
        ),
    "8" => array(
        "1001" => array(88, 90, 95, 77),
        "1002" => array(91, 92, 94, 75),
        "1003" => array(90, 90, 95, 66),
        "1004" => array(90, 92, 88, 70)
        ),
     ..................................
     ..................................
    "10" => array(
        "1001" => array(81, 91, 95, 79),
        "1002" => array(92, 92, 95, 85),
        "1003" => array(91, 90, 91, 62),
        "1004" => array(88, 90, 89, 78)
        )
    );

不是将这些细节放在三维数组中,而是将它放在单维数组中:

//class, Roll_no, physics, chemistry, maths, English
    $marks = array(7, 1001, 90, 95, 98, 78),
                  (7, 1002, 91, 90, 93, 77),
                  (7, 1003, 88, 83, 90, 68),
                  (7, 1004, 90, 91, 90, 80), 
                  (8, 1001, 88, 90, 95, 77),
                  (8, 1002, 91, 92, 94, 75),
                  (8, 1003, 90, 90, 95, 66),
                  (8, 1004, 90, 92, 88, 70)
     );

我的HTML代码是:

<form method="post" name="data" action="mark.php">
Enter class:  
  <select name="class">
      <option value="7"> 7th Class 
      <option value="8"> 8th Class
      <option value="9"> 9th Class
      <option value="10"> 10th Class
  </select>
<br> Enter Roll no: 
  <select name="roll_no">
      <option value="1001"> Roll no:1001
      <option value="1002"> Roll no:1002
      <option value="1003"> Roll no:1003
      <option value="1004"> Roll no:1004
  </select>
<br><input type="submit" value="submit">
 </form>

我的PHP代码是:

if ($_POST) {
    $class = $_POST['class'];
    $roll_no = $_POST['roll_no'];

    $ans1 = $marks[$class][$roll_no][2]; // marks in Physics
    $ans2 = $marks[$class][$roll_no][3]; // marks in chemistry
    $ans3 = $marks[$class][$roll_no][4]; // marks in maths
    $ans4 = $marks[$class][$roll_no][5]; // marks in english

    echo "The marks scored in Physics by Roll no: $roll_no (Class: $class th): $ans1 <br>";
    echo "The marks scored in Chemistry by Roll no: $roll_no (Class: $class th): $ans2 <br>";
    echo "The marks scored in Maths  by Roll no: $roll_no (Class: $class th): $ans3 <br>";
    echo "The marks scored in English by Roll no: $roll_no (Class: $class th): $ans4 <br>";
}

但它显示错误。任何机构都可以帮我纠正代码吗?

1 个答案:

答案 0 :(得分:0)

如果您更喜欢循环

,请尝试使用此代码
$count = sizeof($marks);
for($i=0;$i<$count;$i++)
{
    if($marks[$i][0] == $class && $marks[$i][1] == $$roll_no)
{
   echo 'Yes';
}
}