2013-05-07 30 views
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); 

如何使用显示阵列到表:

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

| ---- Token ----- | num1 | num2 | num3 | num4 | num5 |

| - 技术 - | - 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>"; 
      } 
     } 
... 

但是,结果是:

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

| ---- Token ----- | num1 | num2 | num3 | num4 | num5 |

| - 技术 - | - 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- --- |

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

我该怎么办?

回答

3

试试这个:

$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>"; 
     } 
    } 
+0

三江源评论。有用! – bob 2013-05-07 07:16:47

+0

@DharmeshPatel +1对于简单和爆炸的目标答案。 – 2013-05-07 07:19:24

1

试试这个它是工作。我根据您的要求检查了这一点。

<?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>"; 
?> 
0
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); 

      } 
0

下面的代码,可以帮助你

$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; 
     } 
0

无需为$num阵列...另一种可能的解决方案,它会自动调整为您添加更多令牌的$token array ...

<? $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> 
0

嗨对不起,共享唯一的代码。请参阅下面的代码说明。

我们有两个阵列,令牌和num我们的输出应是

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

SO见我在代码

 <?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 
     }  
     ?> 
+0

请不要只是转储代码。解释它的作用。 – 2016-06-27 20:15:55

0
<?php 
function matrics($int){ 
$j = $int*$int; 
for($i=1;$i<=$j;$i++){ 
    echo $i.' '; 
     if($i%$int==0) 
     echo '<br/>'; 
} 
} 

matrics(4); 

?>