I think your array structure is a bit broken.
It doesn't make much sense both because a variable called $students is keyed by the name of teachers and because it has a false hierarchy (namely the last name being nested inside the first name when they should both be properties of the same parent).
Since you use data for the keys instead of, er, keys, it's not possible to get directly to an element without either knowing its value already or iterating the whole thing. It's going to get ugly and confusing very quickly.
You can relate data in different arrays by their keys in the same way you can relate table in a rdms, and I'd recommend doing so with two arrays that reference eachother's keys like so:
You can use whatever scheme you already have in place to key the student array (if you've got unique pins that's ideal), I just went with initials + full last name for simplicity.
$teachers = array(
'Smith' => array(
'students' => array( 'fbar', 'fmcname' ) // keys of students in this teacher's class
'Crabapple' => array(
'students' => array( 'bjsimpson', 'mprince')
$students = array(
'bjsimpson' => array(
'fname' => 'Bart',
'lname' => 'Simpson',
'teacher' => 'Crabapple' // You can include a reference back if you expect to have to find out the teacher given the student's key
'mprince' => array(
'fname' => 'Martin',
'lname' => 'Prince',
'teacher' => 'Crabapple'
// ... more ...
The loop you asked for with this structure would look like this:
for the record, I think you could iterate your current array with something like
$teacher = $_POST['teacher']; // or whatever
foreach ($teachers[$teacher]['students'] as $student_id)
echo $students[$student_id]['fname'] . ' ' . $students[$student_id]['lname'] . '<br />';
But that's awful code.
foreach ($students[$teacher] as $fname=>$v)
$keys = array_keys($v);
echo $students[$teacher][$fname] . ' ' . $keys . '<br />';