使用php将行转置为列

时间:2015-10-12 10:32:59

标签: php

我的html表格如下:

enter image description here

我试图转置列name_action的值,如下所示:

enter image description here

我无法找到优化算法。

更新:到目前为止,我有一个包含我的数据的多维数组results

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
    )

另一个数组actions包含我的行动列表。

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

我的第一个想法就是“画出”' html选项卡,然后尝试填充它:

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>";
    }  

填充目标表需要太多不必要的循环。 从评论中得到了更好的想法(构建一个以id_demand为关键的多维数组(感谢Niet the Dark Absol)。

0 个答案:

没有答案