【问题标题】:How to display array in matrix table of php如何在php的矩阵表中显示数组
【发布时间】:2013-05-01 01:15:42
【问题描述】:

我有两个数组:

$token = array('technology', 'languange', 'town', 'gadget', 'smartphone');

$num = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25);

如何将该数组显示到表格中:

============================================

|----令牌-----|数字1 |数字2 |数字3 |数字4 |数字5 |

|-技术-|---1----|---2----|---3----|---4----|---5-- --|

|--语言--|---6----|---7----|---8----|---9----|---10 ----|

|-----城镇-----|---11---|---12---|---13---|---14---|-- -15---|

|----小工具----|---16----|---17---|---18---|---19---|--- 20---|

|-智能手机-|---21---|---22---|---23---|---24---|---25---|

============================================

这是我的代码:

...
$counttoken = count($token);
foreach($token as $key=>$value)
        {
            echo "<tr><td>$value</td>";
            for($i=0; $i<$counttoken;$i++)
            {
                echo "<td>" .$num[$i]. "</td>";
            }
        }
...

但是,结果是:

============================================

|----令牌-----|数字1 |数字2 |数字3 |数字4 |数字5 |

|-技术-|---1----|---2----|---3----|---4----|---5-- --|

|--语言--|---1----|---2----|---3----|---4----|---5 ----|

|-----城镇-----|---1----|---2----|---3----|---4--- -|---5----|

|----小工具----|---1----|---2----|---3----|---4----| ---5----|

|-智能手机-|---1----|---2----|---3----|---4----|---5-- --|

============================================

我该怎么办?

【问题讨论】:

    标签: php for-loop html-table


    【解决方案1】:
    <?php
    function matrics($int){
    $j = $int*$int;
    for($i=1;$i<=$j;$i++){
        echo $i.' ';
            if($i%$int==0)
            echo '<br/>';
    }
    }
    
    matrics(4);
    
    ?>
    

    【讨论】:

      【解决方案2】:

      您好,抱歉仅共享代码。请看下面的代码说明。

      我们有两个数组,token 和 num 我们的输出应该是

      技术 1 2 3 4 5 \n 语言 6 7 8 9 10 \n 镇 11 12 13 14 15

      所以在代码中查看我的评论

             <?php
              $token = array('Technology', 'languange', 'town', 'gadget', 'smartphone');
              $num = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25);
              $counttoken = count($token);
              $k = 0;
              foreach($token as $value)
              {
                  echo "<tr><td>".$value."</td>"; // here i am just printing the token value like 'Technology'
                  for($i=0; $i<$counttoken;$i++)
                  {
                      if(isset($num[$i+$k]))
                      {
                          echo "<td>" .$num[$i+$k]. "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";// for first loop of token it will print value of $num[0] to $num[4] means 1,2,3,4,5 and for second loop of token it will print $num[5+0] to $num [5+4] means 6,7,8,9,10 etc
                      }
                      else
                      {
                          echo "<td>&nbsp;</td>";
                      }
                  }
                  $k=$k+$counttoken;     // increase the value of $k for spliting $num array in 5 interval
                  echo "</tr><br>"; // for new line
              }       
              ?>
      

      【讨论】:

        【解决方案3】:

        不需要$num 数组...另一种可能的解决方案,当您向$token 数组添加更多令牌时,它会自动调整...

        <? $token = array('technology', 'languange', 'town', 'gadget', 'smartphone'); ?>
        <? $tc = count($token); ?>
            <table>
                <thead>
                    <tr>
                        <th>Token</th>
                        <? for($j=1;$j<=$tc;$j++):?>
                            <th>num<?= $j;?></th>
                        <? endfor;?>
                    </tr>
                </thead>
                <tbody>
                    <? foreach($token as $tK => $tV):?>
                    <tr>
                        <td><?= $tV?></td>
                        <? for($i = ($tK*$tc + 1); $i <= ($tK*$tc + $tc) ; $i++):?>
                            <td><?= $i;?></td>
                        <? endfor;?>
                    </tr>
                    <? endforeach;?>
                </tbody>
            </table>
        

        【讨论】:

          【解决方案4】:

          试试这个,它正在工作。我根据你的要求检查了这个。

          <?php
          $token = array('technology', 'languange', 'town', 'gadget', 'smartphone');
          
          $num = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25);
          
          $counttoken = count($token);
          echo "<table>";
          foreach($token as $key=>$value)
          {
              echo "<tr><td>$value</td>";
              for($i=0; $i<$counttoken;$i++)
              {
                  echo "<td>" .$num[$i + ($key * $counttoken)]. "</td>";
              }
              echo "</tr>";
          }
          echo "</table>";
          ?>
          

          【讨论】:

            【解决方案5】:

            下面是代码,可以帮助你

            $counttoken = count($token);
            $cnt = 0;
            foreach($token as $key=>$value)
                    {
                        echo "<tr><td>$value</td>";
                        for($i=0; $i<$counttoken;$i++)
                        {
                            if(isset($num[($i+$cnt)]))
                            {
                                echo "<td>" .$num[($i+$cnt)]. "</td>";
                            }
                            else
                            {
                                echo "<td>&nbsp;</td>";
                            }
            
                        }
                        $cnt=$cnt+$counttoken;
                    }
            

            【讨论】:

              【解决方案6】:
              try this   
              
                  $counttoken = count($token);
              
                  foreach($token as $key=>$value)
                          {  
                              echo "<tr><td>$value</td>";
                              for($i=0; $i<$counttoken;$i++)
                              {
                                  echo "<td>" .$num[$i]. "</td>";
                              }
                              $num = array_slice($num,$counttoken); 
              
                          }
              

              【讨论】:

                【解决方案7】:

                试试这个:

                $counttoken = count($token);
                $k=0;
                foreach($token as $key=>$value)
                    {
                        echo "<tr><td>$value</td>";
                        for($i=0; $i<$counttoken;$i++)
                        {
                            echo "<td>" .$num[$k++]. "</td>";
                        }
                    }
                

                【讨论】:

                • @DharmeshPatel +1 以获得简单的目标答案。
                猜你喜欢
                • 2012-05-25
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-03-08
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多