Reputation: 4527
A Have an array
like this
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] =>
)
i would like to format it like this
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
)
)
basically grouping them by their studid
or user_id
now i tried many attempts like
$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++;
}
}
AND
$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;
}else{
$count++;
}
}
But the problem is that it loops only the first part the one with the 0
index and after the condition it stops and does not process or loops on the rest, care to point out what am i doing wrong?
EDIT
So now i edited it and placed the count outside the foreach loop
still the same only iterates on first array
i think after the condition it is not resuming? if i remove the else{}
it outputs a different array structure much worst than what i am aiming.
I tried to echo
out something on the else and it echo's
it i think the problem here is that after the loop went on the condition it returns true and after that goes on to else and does not come back to the if clause
.
Upvotes: 0
Views: 185
Reputation: 11
The point of a foreach is so you don't really need to maintain a count except for perhaps building a new array (however there is shorthand you can use).
I would just do this
$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
}
The if statement seemed redundant and because it prevented count++ when true your loop will get completely out of sync as the foreach automatically moves ahead while your count would not the first time and so will always be one out
Upvotes: 0
Reputation: 1666
Count must be outside the loop:
$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++;
}
Upvotes: 0
Reputation: 20726
There is one error for sure in there, regarding $count
in the second loop:
foreach($subject_student_grades as $value)
{
$count = 0; //Why is that here?
This way, each iteration will overwrite the $count
to 0
, so the $test
array is always addressed with 0
, so element 0
will be the only one in there.
This should read like:
$count = 0;
foreach($subject_student_grades as $value)
{
$uid = $subject_student_grades[$count]->user_id;
Upvotes: 1