开发者

Loop multi dimension array present like html table

$groups = array("Group1"=>array("V1"=>array(1,2,3,4,5),
                                "V2"=>array(2,3,4,5,6),
                                "V3"=>array(3,4,5,6,7)
                                ),
                "Group2"=>array("V1"=>array(11,22,33,44,55),
                                "V2"=>array(11,21,31,41,51),
                                "V3"=>array(12,23,34,45,56)
                                ),
                "Group3"=>array("V1"=>array(1,2,3,4,5),
                                "V2"=>array(1,2,3,4,5),
                                "V3"=>array(1,2,3,4,5)
                                ),
                "Group4"=>array("V1"=>array(1,2,3,4,5),
                                "V2"=>array(1,2,3,4,5),
                                "V3"=>array(1,2,3,4,5)
                                ),
                "Group5"=>array("V1"=>array(1,2,3,4,5),
                                "V2"=>array(1,2,3,4,5),
                                "V3"=>array(1,2,3,4,5)
                                )                
                );

I want to display from this array to html table some thing like this

     Group1          | Group2
     h1|h2|h3|h4|h5  | h1|h2|h3|h4|h5
V1   value of its       value of its
V2   value of its       value of its
V3

    Group3          |  Group4
    h1|h2|h3|h4|h5  |  h1|h2|h3|h4|h5
V1   value of its       value of its
V2   value of its       value of its
V3   value of its       value of its

    Group5          | 
    h1|h2|h3|h4|h5  | 
V1   value of its     
V2   value of its     
V3   value of its     

I am trying to do like this:

 $g_list = array_keys($groups);
echo '<table border=1>';
for($g=0;$g<=sizeOf($groups);$g++){  
    if(($g+1)<sizeOf($groups)){
        echo "<tr>
                <td COLSPAN=6 ALIGN=center>".$g_list[$g]."</td>
                <td COLSPAN=6 ALIGN=center>".$g_list[$g+1]."</td></tr>";
        echo "<tr>
                <td>&nbsp</td><td>h1</td><td>h2</td><td>h3</td><td>开发者_开发技巧;h4</td><td>h5</td>
                <td>&nbsp</td><td>h1</td><td>h2</td><td>h3</td><td>h4</td><td>h5</td>
            </tr>";
         echo "<tr>
                <td>V</td>
                </tr>";
       //todo
    }
}

I could not find it out? Anybody could help me in php?


Here- Not great, but works :)

foreach($groups as $group_name=>$group){

    echo '<table>';
    echo '<tr>';
    echo '<td>h1</td><td>h2</td><td>h3</td><td>h4</td><td>h5</td>';

    foreach($group as $version=>$values){


        echo '<tr><td>'.$version.'</td>';
        foreach($values as $value){
            echo '<td>'.$value.'</td>';
        }
        echo '</tr>';
    }

    echo '</table>';
}


nested loops:

<?php
foreach ($groups as $group => $rows) {
      ....
        foreach ($group as $row_name => $row) {
           ?> <tr><td><?=$row_name ?></TD> <?
           foreach ($row as $value) {
              ?><td><?=$value?></TD><?
           }   
          ...
        }  
      ...
    }
?>

to disp 2 by to use a counting var + use Modulus (%) to know when to start a new line

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜