bob bob - 5 months ago 17
PHP Question

How to display array in matrix table of php

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 that array into the table using :


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

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

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

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

|-----Town-----|---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 is:


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

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

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

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

|-----Town-----|---1----|---2----|---3----|---4----|---5----|

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

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

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


What should i do? Thanks for your help.

Answer

Try this:

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