Jump to content

print duplicate records once from an array into a table


alex.gio

Recommended Posts

[ 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.

Link to comment
Share on other sites

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 />';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.