检查in_array值

Image for explaining my question.

The Image above is what I want to achieve, the only problem is am echoing subjects and topics where the topics go under the subjects, but I want to only echo say English and all topics under English will go under English. But what I have is every topic comes with its own Subjects. Thus from the Image Two English Language dropdowns. Here is the code.

<?php
    $sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2'";
    $result = $pdo->query($sql);
    $result->setFetchMode(PDO::FETCH_ASSOC);
    while($row=$result->fetch()){
        if(!in_array('subject', $row['subject']){
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<ul class="list-unstyled">
    <li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
    </li>
</ul>
</div>
</li>
<?php
        } else {
            echo ""
        }
    }

?>

This should output the subjects and topics and allows for multiple subjects per topic. Also, the echo "" in the else is un-needed.

<?php
    $sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2' ORDER BY subject, topic";
    $result = $pdo->query($sql);
    $result->setFetchMode(PDO::FETCH_ASSOC);
    $lastSubject = '';
    while($row=$result->fetch()){
        if(!in_array('subject', $row['subject']) {
            if($lastSubject != $row['subject']) {
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<?php
            }
?>
<ul class="list-unstyled">
    <li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
    </li>
</ul>
<?php
            if($lastSubject != $row['subject']) {
?>
</div>
<?php
                $lastSubject = $row['subject'];
            }
?>
</li>
<?php
        }
    }

?>