Simplest way is to let SQL do the work.
If you inplode the array into a comma-seperated string, then you use that string as a value list in an SQL query.
$stud_string = implode("," , $unallocatedStuds);
$query1 = "SELECT c.top_stud_id, c.stud_id, c.top_id, c.dt, c.top_pref, (1000 + (4 - s.level) * 10 + (30 - c.top_pref * 5)) AS weight
FROM top_stud c LEFT JOIN student s ON c.stud_id = s.stud_id
WHERE s.stud_id IN (" . $stud_string . ")
ORDER BY weight asc, dt desc";
Not sure that I follow the logic here though?? My guess is that all students in top_stud will always be in student - so why the Left Join?
Anyway, the idea here is to get all the results with 1 query instead of running multiple queries.