$query = "SELECT teacher_name, group_id FROM Teachers ".
"GROUP BY teacher_name ORDER BY group_id";
Grouping the data afterwards by parsing an array is going to be slower and create more overhead than letting SQL do the work.
If you have to change the sort order or grouping interactively, then you should probably store the results in a Javascript array and let the client machine do the work.
If you are trying to page through your data, then you can use a query like this:
$index = "a";
if(isset($_GET['index']))
$index = $_GET['index'];
$query = "SELECT teacher_name, group_id FROM Teachers WHERE teacher_name LIKE '".$index."%' ".
"GROUP BY teacher_name ORDER BY group_id";