arrays - Grouping a php object -
i've php object outputs :
array ( [0] => stdclass object ( [id] => 25 [itemid] => 13 [studentid] => 9 [score] => 22 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst9 [lastname] => studentlast9 ) [1] => stdclass object ( [id] => 26 [itemid] => 13 [studentid] => 10 [score] => 23 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst10 [lastname] => studentlast10 ) [2] => stdclass object ( [id] => 1 [itemid] => 1 [studentid] => 1 [score] => 2 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst1 [lastname] => studentlast1 ) [3] => stdclass object ( [id] => 2 [itemid] => 1 [studentid] => 2 [score] => 3 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst2 [lastname] => studentlast2 ) [4] => stdclass object ( [id] => 9 [itemid] => 5 [studentid] => 1 [score] => 6 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst1 [lastname] => studentlast1 ) [5] => stdclass object ( [id] => 10 [itemid] => 5 [studentid] => 2 [score] => 7 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst2 [lastname] => studentlast2 ) [6] => stdclass object ( [id] => 17 [itemid] => 9 [studentid] => 1 [score] => 10 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst1 [lastname] => studentlast1 ) [7] => stdclass object ( [id] => 18 [itemid] => 9 [studentid] => 2 [score] => 11 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst2 [lastname] => studentlast2 ) [8] => stdclass object ( [id] => 3 [itemid] => 2 [studentid] => 1 [score] => 3 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst1 [lastname] => studentlast1 ) [9] => stdclass object ( [id] => 4 [itemid] => 2 [studentid] => 2 [score] => 4 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst2 [lastname] => studentlast2 ) [10] => stdclass object ( [id] => 11 [itemid] => 6 [studentid] => 1 [score] => 7 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst1 [lastname] => studentlast1 ) [11] => stdclass object ( [id] => 12 [itemid] => 6 [studentid] => 2 [score] => 8 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst2 [lastname] => studentlast2 ) [12] => stdclass object ( [id] => 19 [itemid] => 10 [studentid] => 1 [score] => 11 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst1 [lastname] => studentlast1 ) [13] => stdclass object ( [id] => 20 [itemid] => 10 [studentid] => 2 [score] => 12 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst2 [lastname] => studentlast2 ) [14] => stdclass object ( [id] => 7 [itemid] => 4 [studentid] => 1 [score] => 5 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst1 [lastname] => studentlast1 ) [15] => stdclass object ( [id] => 8 [itemid] => 4 [studentid] => 2 [score] => 6 [imagealt] => [timemodified] => 0 [data] => [firstname] => studentfirst2 [lastname] => studentlast2 ) )
as, 1 can see, there 2 studentid
having different itemid
. want group them , print table like:
itemidid1 itemidid2 itemidid3 studentid1 x1 x2 x3 studentid2 y1 y2 y3
i'm trying following code, displays values 1 below other.
<?php foreach ($hits $hit): ?> <?php if ($hit->studentid == $current_id): ?> <td> <?php echo $hit->score; ?> </td> <?php else: ?> <?php if ($current_id != -1): ?> </tr> <?php endif; ?> <?php $current_id = $hit->studentid; ?> <tr> <td><a href="#" id="<?php echo $hit->studentid; ?>" class="student-grade-data"> <?php echo $hit->firstname.' '.$hit->lastname; ?> </a></td> <td> <?php echo $hit->score; ?> </td> <?php endif; ?> <?php endforeach; ?>
loop on objects , collect objects in 2 arrays like:
$bystudentid[$hit->studentid] = $hit; $byitemid[$hit->itemid] = $hit;
then can columns array_keys($byitemid)
, finaly build table looping on $bystudenid
Comments
Post a Comment