获取数组键并与php中不同数组中的键进行比较

时间:2013-05-13 06:38:27

标签: php arrays codeigniter

我有一个存储主题的表和一个存储学生在考试中得分的结果的表,使用视图表显示结果,我希望系统显示( - )学生没有把这个主题和分数放在他主持的地方。

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

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 ) )

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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>

请花时间优化此代码