0

由于某种原因,这个数组不断重复第一行到第二行,第一行和第二行到第三行,依此类推。

unset ($grade); $grade = array();
foreach ($row as $grade => $value) {
  if (substr($grade, 0,1)== "y") {
  if ($grade == "y1") {if ($value == "x") {$grades[] = "1";}}
  if ($grade == "y2") {if ($value == "x") {$grades[] = "2";}}
  if ($grade == "y3") {if ($value == "x") {$grades[] = "3";}}
  if ($grade == "y4") {if ($value == "x") {$grades[] = "4";}}
  if ($grade == "y5") {if ($value == "x") {$grades[] = "5";}}
  if ($grade == "y6") {if ($value == "x") {$grades[] = "6";}}
  if ($grade == "y7") {if ($value == "x") {$grades[] = "7";}}
}}
$grades_string = implode(", ", $grades);

dB 行的标题为 y1、y2 等。如果需要,我可以向某人展示一个链接,其中包含我的意思是错误的示例。

4

2 回答 2

4

这是因为你创造$grade = array();,但你想要创造$grades = array();

更正此语法错误,然后它应该可以正常工作。

您也可以移至if ($value == "x")此,if (substr($grade, 0,1)== "y")使您的代码更简洁、更快。在你的情况下:

if (substr($grade, 0,1)== "y" && $value == "x") {
    if ($grade == "y1") {$grades[] = "1";}
    ...
于 2013-01-28T00:53:44.097 回答
0

我将重写循环以使其更具可读性/更有意义,您仍然需要您清除问题所在,然后我将更新我的答案。

foreach ($row as $grade => $value) {
    if (substr($grade, 0,1)== "y" AND $value == "x") {
        if ($grade == "y1") {$grades[] = "1";}
        elseif ($grade == "y2") {$grades[] = "2";}
        elseif ($grade == "y3") {$grades[] = "3";}
        elseif ($grade == "y4") {$grades[] = "4";}
        elseif ($grade == "y5") {$grades[] = "5";}
        elseif ($grade == "y6") {$grades[] = "6";}
        elseif ($grade == "y7") {$grades[] = "7";}
    }
}
于 2013-01-28T00:53:09.100 回答