A 有一个array
这样的
Array
(
[0] => stdClass Object
(
[user_id] => 3035
[category] => Preliminary
[value] => 0
[fullname] =>
[studid] => 1202924
[remarks] =>
)
[1] => stdClass Object
(
[user_id] => 3035
[category] => Midterm
[value] => 0
[fullname] =>
[studid] => 1202924
[remarks] =>
)
[2] => stdClass Object
(
[user_id] => 3035
[category] => Semi-Finals
[value] => 0
[fullname] =>
[studid] => 1202924
[remarks] =>
)
[3] => stdClass Object
(
[user_id] => 3035
[category] => Finals
[value] => 0
[fullname] =>
[studid] => 1202924
[remarks] =>
)
[4] => stdClass Object
(
[user_id] => 3035
[category] => Remedial
[value] => 0
[fullname] =>
[studid] => 1202924
[remarks] =>
)
[5] => stdClass Object
(
[user_id] => 3036
[category] => Preliminary
[value] => 0
[fullname] =>
[studid] => 1202925
[remarks] =>
)
[6] => stdClass Object
(
[user_id] => 3036
[category] => Midterm
[value] => 0
[fullname] =>
[studid] => 1202925
[remarks] =>
)
[7] => stdClass Object
(
[user_id] => 3036
[category] => Semi-Finals
[value] => 0
[fullname] =>
[studid] => 1202925
[remarks] =>
)
[8] => stdClass Object
(
[user_id] => 3036
[category] => Finals
[value] => 0
[fullname] =>
[studid] => 1202925
[remarks] =>
)
[9] => stdClass Object
(
[user_id] => 3036
[category] => Remedial
[value] => 0
[fullname] =>
[studid] => 1202925
[remarks] =>
)
我想这样格式化
Array
(
[0] => Array
(
[fullname] =>
[studid] => 1202924
[user_id] => 3035
[Preliminary] => 0
[Midterm] => 0
[Semi-Finals] => 0
[Finals] => 0
[Remedial] => 0
),
[1] => array
(
[fullname] =>
[studid] => 1202925
[user_id] => 3036
[preliminary] => 0
[midterm] => 0
[semi-finals] => 0
[finals] => 0
[remedial] => 0
)
)
基本上按他们studid
或user_id
现在我尝试了很多尝试,比如
$count = 0;
$uid = $subject_student_grades[$count]->user_id;
for($x=0;$x < count($subject_student_grades);$x++)
{
if($uid == $subject_student_grades[$x]->user_id)
{
$test[$count]['fullname'] = $subject_student_grades[$x]->fullname;
$test[$count]['studid'] = $subject_student_grades[$x]->studid;
$test[$count]['user_id'] = $subject_student_grades[$x]->user_id;
$test[$count][$subject_student_grades[$x]->category] = $subject_student_grades[$x]->value;
}else{
$count++;
}
}
和
$计数 = 0; foreach($subject_student_grades 作为 $value) {
$uid = $subject_student_grades[$count]->user_id;
if($uid == $value->user_id)
{
$test[$count]['fullname'] = $value->fullname;
$test[$count]['studid'] = $value->studid;
$test[$count]['user_id'] = $value->user_id;
$test[$count][$value->category] = $value->value;
}else{
$count++;
}
}
但问题是它只循环带有0
索引的第一部分,并且在条件停止并且不处理或循环其余部分之后,注意指出我做错了什么?
编辑
所以现在我编辑它并将计数放在foreach loop
仍然相同的唯一迭代之外,array
我认为在它不恢复的条件之后?如果我删除它,else{}
它输出的数组结构比我的目标差得多。
我试图echo
在 else 上输出一些东西,echo's
我认为这里的问题是,在循环进入条件后它返回 true,然后继续到 else 并且不会返回到if clause
.