我有一个foreach循环,用于数组$students,我试图将从表单中获得的paramater$find与$students数组的$key值进行比较-
我把这个从芬兰语翻译过来,所以可能会有错别字,但问题在于语法...
//values come from a file
$students[$key] = array('key' => $key, 'name' => $name, 'occ' => $occ);
foreach ($students as $value) {
//This doesn't work - $find comes from a form
if ($value["key"] != $find) {
$phase= "Not found";
$enroll= "";
//echo "$phase $enroll";
continue;
}
//This works
elseif ($value["key"] == $find) {
$phase= $value["name"] . "(" . $value["key"] . "):";
if ($value["occ"] == "1") {
$enroll= " yes";
continue;
}
elseif ($value["occ"] == "0") {
$enroll= "no";
continue;
}
}
//It prints out for example "John(1234): yes"
//But nothing if the studentnumber = key doesn't match..
echo "$phase $enroll";
}
你不需要检查$value["key"]
两次
$students[$key] = array('key' => $key, 'name' => $name, 'occ' => $occ);
foreach ( $students as $value ) {
$phase= "Not found";
$enroll= "";
if ( $value["key"] == $find ) {
$phase= $value["name"] . "(" . $value["key"] . "):";
$enroll = ( $value["occ"] == "1" ) ? " yes" : "no";
}
echo "$phase $enroll";
}