alex.gio Posted November 20, 2010 Share Posted November 20, 2010 [ Newbie Alert ] Hi i have an array of 39 arrays. Only 5 are shown below so that you have an idea before i ask my question. <? array(39) { [0]=> array(3) { [0]=> string(18) "unit name 1" [1]=> string(9) "category name 1" [2]=> string(20) "Course 1" } [1]=> array(3) { [0]=> string(54) "unit name 2" [1]=> string(15) "category name 2" [2]=> string(20) "Course 1" } [2]=> array(3) { [0]=> string(29) "unit name 3" [1]=> string(15) "category name 2" [2]=> string(20) "Course 1" } [3]=> array(3) { [0]=> string(64) "unit name 4" [1]=> string(30) "category name 3" [2]=> string(20) "Course 2" } [4]=> array(3) { [0]=> string(57) "unit name 5" [1]=> string(24) "category name 1" [2]=> string(20) "Course 1" } [5]=> array(3) { [0]=> string(50) "unit name 6" [1]=> string(24) "category name 4" [2]=> string(20) "Course 2" } } i want a php solution to print category names in table headers , unit names in table rows and course name(s) in table caption.I have tried foreach loop but how to remove duplicate entries as you can see category name 1 and 2 will become two separate headers instead of one and same is the case with Course 1 and 2. i have googled but could'nt find any solution. i don't want to remove duplicate entries , all i want to display them once in a table as described above. Quote Link to comment https://forums.phpfreaks.com/topic/219274-print-duplicate-records-once-from-an-array-into-a-table/ Share on other sites More sharing options...
sasa Posted November 20, 2010 Share Posted November 20, 2010 try <?php $test = array( array("unit name 1", "category name 1", "Course 1"), array("unit name 2", "category name 2", "Course 1"), array("unit name 3", "category name 2", "Course 1"), array("unit name 4", "category name 3", "Course 2"), array("unit name 5", "category name 1", "Course 1"), array("unit name 6", "category name 4", "Course 2") ); foreach ($test as $data){ $tmp[$data[2]][$data[1]][] = $data[0]; } foreach ($tmp as $course => $data){ echo $course, '<br /><table border="3">'; $tbl = array(); $i = 0; foreach ($data as $cat => $unit){ $i++; $tbl [0][$i] = $cat; $tbl[1][$i] = implode('<br />', $unit); } echo '<tr><td>', implode('</td><td>', $tbl[0]),'</td></tr>'; echo '<tr><td>', implode('</td><td>', $tbl[1]),'</td></tr>'; echo '</table><br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219274-print-duplicate-records-once-from-an-array-into-a-table/#findComment-1137104 Share on other sites More sharing options...
alex.gio Posted November 21, 2010 Author Share Posted November 21, 2010 sasa You are not only a PHP Guru but also a great person who took time to write complete code for this. i am very grateful to you. Thanks a lot. Alex Quote Link to comment https://forums.phpfreaks.com/topic/219274-print-duplicate-records-once-from-an-array-into-a-table/#findComment-1137364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.