使用php将行转置为列

My html table looks like this :

enter image description here

I'm trying to transpose the values of the column name_action as follows :

enter image description here

I can't figure out an optimized algorithm to do so.

Update : so far i had a multidimensional array resultsthat contained my data :

Array
(
[0] => Array
    (
        [id_demand] => 14
        [id_projet] => 81537
        [nom] => software_update
        [open_demand_date] => 09/12/10
        [close_date] => 06/04/11
        [name_action] => db_update
        [total] => 1
    )

[1] => Array
    (
        [id_demand] => 14
        [id_projet] => 81537
        [nom] => software_update
        [open_demand_date] => 09/12/10
        [close_date] => 06/04/11
        [name_action] => closure
        [total] => 0
    )

another array actionscontains the list of my actions.

Array
(
[0] => db_update
[1] => closure
[2] => validation
[3] => networking
[4] => os_creation
[5] => os_installation

)

the first idea i had is to 'draw' the html tab, then try to fill it :

foreach ($result[0] as $k=>$v){
        if ($k == 'name_action') break;
        else 
            echo "<th>".$k."</th>"; 
        }
    foreach ($actions as $k => $v){
        echo "<th>".$v."</th>";
    }  

filling the target table required too much unnecessary looping. Got a better idea from the comments (building a multidimensional array with id_demand as the key (thanks to Niet the Dark Absol)).