How to display array in matrix table of php
Solution 1
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>";
}
}
Solution 2
Try this it is working. I checked this as per 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>";
?>
bob
Updated on June 04, 2022Comments
-
bob almost 2 years
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?