我有一个对象学生,其变量包括student-> name,student-> age,student-> marks,student-> id
我试过这个: 我这样做是为了得到排序的数组
foreach($student as $s){
array_push($student_id_array,$s->id) }
sort($student_id_array);
所以我有学生的排序ID数组, 但是如何以$ student_id_array的相同顺序显示100个$ student实例?
答案 0 :(得分:1)
这将根据每个$students
$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;
}
答案 1 :(得分:1)
您可以按顺序显示数组(创建数组的顺序),如下所示:
foreach($student_id_array as $student) {
echo 'student #' . $student->id . ', name: ' . $student->name . '<br/>';
}