-1

我有一个存储提供的科目的表格和一个存储学生在考试中得分的结果的表格,在使用视图表显示结果时,我希望系统显示一个 (-) 学生不参加该科目和他正在学习的主题的分数。

存储的主题的样本数组是:

Array (
[0] => stdClass Object ( [subCode] => 101 [subName] => English )
[1] => stdClass Object ( [subCode] => 102 [subName] => Kiswahili )
[2] => stdClass Object ( [subCode] => 121 [subName] => Mathematics )
[3] => stdClass Object ( [subCode] => 231 [subName] => Biology )
[4] => stdClass Object ( [subCode] => 232 [subName] => Physics )
[5] => stdClass Object ( [subCode] => 233 [subName] => Chemistry )
[6] => stdClass Object ( [subCode] => 311 [subName] => History )
[7] => stdClass Object ( [subCode] => 312 [subName] => Geography )
[8] => stdClass Object ( [subCode] => 313 [subName] => CRE )
[9] => stdClass Object ( [subCode] => 443 [subName] => Agriculture )
[10] => stdClass Object ( [subCode] => 565 [subName] => Business Studies ) ) 

包含要显示的学生结果的数组是:

Array ( [0] => stdClass Object ( [admNo] => 2129 [subCode] => 101 [score] => 78 ) [1] => stdClass Object ( [admNo] => 2129 [subCode] => 102 [score] => 80 ) [2] => stdClass Object ( [admNo] => 2129 [subCode] => 121 [score] => 70 ) [3] => stdClass Object ( [admNo] => 2129 [subCode] => 231 [score] => 76 ) [4] => stdClass Object ( [admNo] => 2129 [subCode] => 233 [score] => 76 ) [5] => stdClass Object ( [admNo] => 2129 [subCode] => 311 [score] => 85 ) [6] => stdClass Object ( [admNo] => 2129 [subCode] => 313 [score] => 90 ) [7] => stdClass Object ( [admNo] => 2129 [subCode] => 565 [score] => 80 ) [8] => stdClass Object ( [admNo] => 4093 [subCode] => 101 [score] => 60 ) [9] => stdClass Object ( [admNo] => 4093 [subCode] => 102 [score] => 70 ) [10] => stdClass Object ( [admNo] => 4093 [subCode] => 121 [score] => 85 ) [11] => stdClass Object ( [admNo] => 4093 [subCode] => 231 [score] => 80 ) [12] => stdClass Object ( [admNo] => 4093 [subCode] => 232 [score] => 80 ) [13] => stdClass Object ( [admNo] => 4093 [subCode] => 233 [score] => 80 ) [14] => stdClass Object ( [admNo] => 4093 [subCode] => 312 [score] => 95 ) [15] => stdClass Object ( [admNo] => 4093 [subCode] => 565 [score] => 86 ) )

我需要一个解决方案,其中视图显示 (-),其中未在结果数组中找到主题数组中的子代码。

4

2 回答 2

0

结合数组并根据子代码排序,然后在最终数组中只传递一次的元素就是您要查找的元素。

如果您使用的是 java,您可以使用comparablecomparator根据数组中的一个实例进行排序。

于 2013-05-13T06:59:28.630 回答
0
<?php
$subjects = Array (
array ( 'subCode' => 101, 'subName' => 'English' ),
array ( 'subCode' => 102, 'subName' => 'Kiswahili' ),
array ( 'subCode' => 121, 'subName' => 'Mathematics' ),
array ( 'subCode' => 231, 'subName' => 'Biology' ),
array ( 'subCode' => 232, 'subName' => 'Physics' ),
array ( 'subCode' => 233, 'subName' => 'Chemistry' ),
array ( 'subCode' => 311, 'subName' => 'History' ),
array ( 'subCode' => 312, 'subName' => 'Geography' ),
array ( 'subCode' => 313, 'subName' => 'CRE' ),
array ( 'subCode' => 443, 'subName' => 'Agriculture' ),
array ( 'subCode' => 565, 'subName' => 'Business Studies' ) 
) ;



$scores = Array ( '0' => array ( 'admNo' => 2129, 'subCode' => 101, 'score' => 78 ), '1' => array ( 'admNo' => 2129, 'subCode' => 102, 'score' => 80 ), '2' => array ( 'admNo' => 2129, 'subCode' => 121, 'score' => 70 ), '3' => array ( 'admNo' => 2129, 'subCode' => 231, 'score' => 76 ), '4' => array ( 'admNo' => 2129, 'subCode' => 233, 'score' => 76 ), '5' => array ( 'admNo' => 2129, 'subCode' => 311, 'score' => 85 ), '6' => array ( 'admNo' => 2129, 'subCode' => 313, 'score' => 90 ), '7' => array ( 'admNo' => 2129, 'subCode' => 565, 'score' => 80 ), '8' => array ( 'admNo' => 4093, 'subCode' => 101, 'score' => 60 ), '9' => array ( 'admNo' => 4093, 'subCode' => 102, 'score' => 70 ), '10' => array ( 'admNo' => 4093, 'subCode' => 121, 'score' => 85 ), '11' => array ( 'admNo' => 4093, 'subCode' => 231, 'score' => 80 ), '12' => array ( 'admNo' => 4093, 'subCode' => 232, 'score' => 80 ), '13' => array ( 'admNo' => 4093, 'subCode' => 233, 'score' => 80 ), '14' => array ( 'admNo' => 4093, 'subCode' => 312, 'score' => 95 )/*, '15' => array ( 'admNo' => 4093, 'subCode' => 565, 'score' => 86 )*/);

$students =  array(2129,4093);
print_r($students);

function took_test($scores,$admNo, $subj){
    $r = FALSE;
    foreach($scores as $score){
        if($score['subCode'] == $subj && $score['admNo'] == $admNo){
            $r =  "<td>".$score['score']."</td>";
        }
    }
    return $r;
}
?>

<table border="1">
<tr>
<td>Student AdmNo</td>
<?php
foreach($subjects as $subj){
    echo "<td>".$subj['subName']."</td>";
}
?>
</tr>
<?php
foreach($students as $s){
    echo "<tr>";
    echo "<td>".$s."</td>";
    foreach($subjects as $subj){
        if(!took_test($scores,$s,$subj['subCode'])){
            echo "<td>-</td>";
            }else{
            echo took_test($scores,$s,$subj['subCode']);
        }
    }
    echo "</tr>";
}
?>
</table>

<pre><?php
print_r($subjects);

print_r($scores);
?></pre>

请花时间优化此代码

于 2013-05-14T12:46:36.750 回答