如何在PHP中单击按钮时保留多选框的选择?

I have the below code:

<?php
    ini_set('display_errors',"1");

    $select = '<select multiple name = "select3[]">';

    $lines = file('/volumes/file-vault-projects/project-list.txt');
    $fifth_column = array();
    foreach ($lines as $line){
        $parts = preg_split('/\s+/', $line);
        $count = 0;
        foreach ($parts as $partVal){
            if ((in_array($partVal, $fifth_column) == FALSE) && $count == 4){
                $fifth_column[] = $partVal;
                sort($fifth_column);
            }
            $count++;
        }
    }

    foreach($fifth_column as $value){


        $select .= "<option value='".$value."'>".$value."</option>";
    }

    $select .= '</select>';

    echo $select;





?>

On a button click the selections are cleared. How can I keep the options selected? I have managed to keep other things selected but that was in HTML. I am unsure how to do it in PHP.

I have numerous if statements to see if anything has been selected. For example:

if($_POST){
    if(isset($_POST['Apply']) && 
      (isset($_POST['Finished']) && $_POST['Finished'] == 'Finished') && 
        !isset($_POST['dates'])&&
       !empty($_POST['select3']) ) {
Apply();
}

function Apply() {

     foreach($_POST['select3'] as $project1) 
     {
       $searchthis = "  finished    ";
       $project = $_POST['select3'];
       $matches = array();
       $handle = @fopen("/volumes/file-vault-projects/project-list.txt", "r");

        if ($handle) {
                        while (!feof($handle)) {
                            $buffer = fgets($handle);
                                if(strpos ($buffer, $project1) &&
                                    strpos($buffer, $searchthis) !== FALSE)
                                    $matches[] = $buffer;

                                }
            fclose($handle);
        }




            foreach ($matches as $child)
            {
             $col1 = explode ("\t", $child);
         echo '<tr>';
                    echo '<td>',htmlspecialchars($col1[0]). "<br />";
                    echo '</td>';
                    echo '</td><td>', htmlspecialchars($col1[1]). "<br />";
                    echo '</td>';
                    echo '</td><td>', htmlspecialchars($col1[2]). "<br />";
                    echo '</td>';
                    echo '</td><td>', htmlspecialchars($col1[3]). "<br />";
                    echo '</td>';
                    echo '</td><td>', htmlspecialchars($col1[4]). "<br />";
                    echo '</td>';
                    echo '</td><td>', htmlspecialchars($col1[5]). "<br />";
                    echo '</td></tr>';

            }


}
    echo '</table>';
    echo "</div>";
}

Thanks

If you want the options that were submitted in the form to be pre-selected when the page is redisplayed, add the selected attribute to the option based on whether it's in $_POST['select3'].

foreach ($fifth_column as $value) {
    $selected = (isset($_POST['select3']) && in_array($value, $_POST['select3'])) ? 'selected' : '';
    $select .= "<option value='$value' $selected>$value</option>";
}