How to display array in php matrix table

I have two arrays:

$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); 

How to display this array in a table using:

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

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

| -Technology- | --- 1 ---- | --- 2 ---- | --- 3 ---- | --- 4 ---- | --- 5-- - |

| --Language-- | --- 6 ---- | --- 7 ---- | --- 8 ---- | --- 9 ---- | --- 10 ---- |

| ----- City ----- | --- 11 --- | --- 12 --- | --- 13 --- | --- 14 --- | - -15 --- |

| ---- Gadget ---- | --- 16 ---- | --- 17 --- | --- 18 --- | --- 19 --- | --- 20 --- |

| -Smartphone- | --- 21 --- | --- 22 --- | --- 23 --- | --- 24 --- | --- 25 --- |

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

This is my code:

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

But the result:

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

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

| -Technology- | --- 1 ---- | --- 2 ---- | --- 3 ---- | --- 4 ---- | --- 5-- - |

| --Language-- | --- 1 ---- | --- 2 ---- | --- 3 ---- | --- 4 ---- | --- 5 ---- |

| ----- City ----- | --- 1 ---- | --- 2 ---- | --- 3 ---- | --- 4 --- - | --- 5 ---- |

| ---- Gadget ---- | --- 1 ---- | --- 2 ---- | --- 3 ---- | --- 4 ---- | --- 5 ---- |

| -Smartphone- | --- 1 ---- | --- 2 ---- | --- 3 ---- | --- 4 ---- | --- 5-- - |

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

What should I do?

+4
source share
7 answers

Try the following:

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

Try this, it works. I checked it according to your requirement.

 <?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>"; ?> 
+1
source
 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
source

Below is the code that may help you

 $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
source

There is no need for the $num array ... another possible solution that will automatically change when you add more tokens to the $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
source

Hi Sorry for the generic code. see code explanation below.

We have two arrays, the token and the number of our output should be

technology 1 2 3 4 5 \ n languange 6 7 8 9 10 \ n city 11 12 13 14 15

SO see my comment in code

  <?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
source
 <?php function matrics($int){ $j = $int*$int; for($i=1;$i<=$j;$i++){ echo $i.' '; if($i%$int==0) echo '<br/>'; } } matrics(4); ?> 
0
source

Source: https://habr.com/ru/post/1479481/


All Articles