4

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?

7 Answers 7

5

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>";
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

@DharmeshPatel +1 for simple & bang on target answer.
1

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

Comments

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

            }

Comments

0

Below is code, that can 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;
        }

Comments

0

No need for the $num array... another possible solution, which will automatically adjust as 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>

Comments

0

Hi Sorry for share only code. see below the code explanation.

We have two arrays, token and num our output should be

technology 1 2 3 4 5 \n languange 6 7 8 9 10 \n town 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
        }       
        ?>

Comments

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

matrics(4);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.