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++;
}
}
和
$ 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
在其他地方发现了一些内容echo's
我觉得这里的问题是在循环进入条件后它返回true然后继续到其他地方并且不回到if clause
。
答案 0 :(得分:1)
对于第二个循环中的$count
,确实存在一个错误:
foreach($subject_student_grades as $value)
{
$count = 0; //Why is that here?
这样,每次迭代都会覆盖$count
到0
,因此$test
数组始终使用0
进行处理,因此元素0
将是那里唯一一个。
这应该是:
$count = 0;
foreach($subject_student_grades as $value)
{
$uid = $subject_student_grades[$count]->user_id;
答案 1 :(得分:0)
计数必须在循环之外:
$count = 0;
foreach($subject_student_grades as $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;
}
$count++;
}
答案 2 :(得分:0)
foreach的观点是,除了可能构建一个新数组之外,你不需要保持计数(但是你可以使用简写)。
我会这样做
$test = array();
foreach($subject_student_grades as $value) {
$temp = array();
$temp['fullname'] = $value->fullname;
$temp['studid'] = $value->studid;
$temp['user_id'] = $value->user_id;
$temp[$value->category] = $value->value;
$test[] = $temp; # This is shorthand to assign the next numerical index
}
if语句似乎是多余的,因为它阻止了count ++ when true,你的循环将完全不同步,因为foreach会自动向前移动,而你的计数不会是第一次,所以总是只有一个