-3

I want to display my two dimensional array into html table, where similar values should be combined/merged into one cell. For instance, I have an array:-

$final_arr = array(
    array(
        "id"      => "106",
        "name"    => "Fashion",
        "asset"   => "project",
        "assetid" => "2708",
        'version' => "2.16.22"
    ),
    array(
        "id"      => "2",
        "name"    => "Sports",
        "asset"   => "project",
        "assetid" => "2758",
        'version' => "2.16.26"
    ),
    array(
        "id"      => "106",
        "name"    => "Fashion",
        "asset"   => "tracking",
        "assetid" => "2758",
        'version' => "2.15.16"
    ),
    array(
        "id"      => "153",
        "name"    => "Giyan",
        "asset"   => "character",
        "assetid" => "2694",
        'version' => "2.20.6"
    )
); 

Now I want to display the above result set into an html table as :-

 ID          Asset             Version  

106          project           2.16.22
             tracking          2.16.26  
2            Sports            2.15.16
153          Giyan             2.20.6   

Please can someone provide their insight on the same on how this can be achieved?

Thanks much!!

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
Troy
  • 3
  • 3
  • 3
    SO is not about people writing code for you !! you should search and do some effort then we can help you, a simple search gives http://stackoverflow.com/questions/4529965/how-to-convert-an-array-to-a-html-table – Issam Zoli Jul 05 '14 at 19:38
  • looping through the array is not a problem, I am stuck during merging the similar ID into one row (as provided (html table) in the above example) – Troy Jul 05 '14 at 19:49
  • How are you trying to combine them? – a coder Jul 05 '14 at 19:49
  • Side note. This type of thing would practically be on _page one_ of any PHP tutorial/book/intro/etc. There are probably a hundred thousand examples of this online, on php.net, on _this site_.. I am willing to bet there's even something on yahoo answers on how to do this. One search for "PHP arrays" would give you a million different, _correct_ answers on how to do this. – Yes Barry Jul 05 '14 at 19:49
  • @Tarun don't give us the output of your array in your example, give us the _array_ itself.. e.g. `$myArray = array(0 => array('id' => 153, 'name' => 'Giyah')); // etc`. – Yes Barry Jul 05 '14 at 19:52
  • Thanks, here is the array : $final_arr = array( array("id"=>"106","name"=>"Fashion","asset"=>"project","assetid"=>"2708","version"=>"2.16.22"), array("id"=>"2","name"=>"Sports","asset"=>"project","assetid"=>"2758","version"=>"2.16.26"), array("id"=>"106","name"=>"Fashion","asset"=>"tracking","assetid"=>"2758","version"=>"2.15.16"), array("id"=>"153","name"=>"Giyan","asset"=>"character","assetid"=>"2694","version"=>"2.20.6")); – Troy Jul 05 '14 at 20:03
  • @Tarun Against my better judgement, I answered your question. I didn't test it but I know it works. – Yes Barry Jul 05 '14 at 20:16

1 Answers1

0

I'm probably gonna regret answering this, but here's a simple, easy-to-understand solution.

Create a new array using the id as the index (key).

$newArray = array();
foreach ($yourArray as $array) {
    $filter = $array;
    unset($filter['id']);
    $newArray[$array['id']][] = $filter;
}

Then in your table:

<table>
<thead>
    <tr>
        <th>ID</th>
        <th>Asset</th>
        <th>Version</th>
    </tr>
</thead>
<tbody>
<?php foreach ($newArray as $id => $array): ?>
    <?php $count = 0; ?>
    <?php foreach ($array as $arr): ?>
    <tr>
        <td><?= (++$count <= 1) ? $id : ''; ?></td>
        <td><?= $arr['asset']; ?></td>
        <td><?= $arr['version']; ?></td>
    </tr>
    <?php endforeach; ?>
<?php endforeach; ?>
</tbody>
</table>
Yes Barry
  • 9,514
  • 5
  • 50
  • 69