I have an object student which has variables like student->name , student->age , student->marks , student->id
I have tried this : i have done this to get sorted array of ids
foreach($student as $s){
array_push($student_id_array,$s->id) }
sort($student_id_array);
So i have the array of sorted id of student, but how can i display 100 instances of $student in the same order a $student_id_array ?
This will sorts all your $students
array based on each $student->id
:
$stud = array (
(object)array('name' => 'John Doe', 'id' => 1),
(object)array('name' => 'Jane Doe', 'id' => 3),
(object)array('name' => 'Luke Will', 'id' => 4),
(object)array('name' => 'Eric Sting', 'id' => 2)
);
$Students = array();
foreach($stud as $s){
$Students[$s->id] = $s;
}
ksort($Students);
foreach($Students as $s){
echo $s->name;
}
You can display the array in order (the order you created the array) by looping through it like this:
foreach($student_id_array as $student) {
echo 'student #' . $student->id . ', name: ' . $student->name . '<br/>';
}