嗨我有这样的数组
$subject_names[7]="English";
$subject_names[11]="Software Engeneering";
//Student can choose multiple subjects and each subject have int_mark and ext_mark
$results['Matt'][7]['int_mark'] =15;
$results['Matt'][7]['ext_mark'] =55;
$results['Josh'][7]['int_mark'] =12;
$results['Josh'][7]['ext_mark'] =45;
$results['Josh'][11]['int_mark'] =14;
$results['Josh'][11]['ext_mark'] = 52;
// the array is to maintain crosstab format
要打印这个我做了
echo "Student Name\t";
foreach($subject_names as $subject_name)
{
echo "$subject_name\t";
}
echo "<br>";
foreach ($results as $student_name => $subjects)
{
echo "$student_name\t";
foreach($subject_names as $subject_id => $sub_name){
foreach ($subjects[$subject_id] as $mark){
echo "$mark\t";
}
}
echo "<br>";
}
作为学生“马特”没有subject_id 11它给了我一个错误通知
注意:未定义的偏移量:11
如果学生没有该主题,我如何忽略它并打印N / A
感谢您的任何帮助和建议
答案 0 :(得分:1)
您可以将isset()
与count()
一起使用: -
if(isset($subjects[$subject_id]) && count($subjects[$subject_id])>0){
foreach ($subjects[$subject_id] as $mark){
echo "$mark\t";
}
}
您也可以!empty()
使用count()
检查: -
if(!empty($subjects[$subject_id]) && count($subjects[$subject_id])>0){
foreach ($subjects[$subject_id] as $mark){
echo "$mark\t";
}
}