Array within array

i fetched 6 rows from database in a varible $fetch

when i applies var_dump(). it shows the result like this

Array (
 [0] => Array ( [sno] => 1 [num1] => 5 [num2] => 5 )
 [1] => Array ( [sno] => 2 [num1] => 5 [num2] => 10 )
 [2] => Array ( [sno] => 3 [num1] => 10 [num2] => 4 )
 [3] => Array ( [sno] => 4 [num1] => 7 [num2] => 6 )
 [4] => Array ( [sno] => 5 [num1] => 9 [num2] => 4 )
 [5] => Array ( [sno] => 6 [num1] => 40 [num2] => 5 )
 )

now i want to show this in a table like 1st three key values in first row, then next three key values in next row and so on …

like this

<tr>
<td>5 x5 =</td><td>input text field</td>
<td>5 x 10 =</td><td>input text field</td>
<td>10 x 4 =</td><td>input text field</td>
<tr>
<tr>
<td>7 x 6 =</td><td>input text field</td>
<td>9 x 4 =</td><td>input text field</td>
<td>40 x 5 =</td><td>input text field</td>
<tr>

hope you get , what i want to achieve…

now i tried this

<?php
foreach($fetch as $key => $value)
	{
?>
           <tr>

        	<?php
                   for($i=0;$i<3;$i++)
		     {
	         ?>
		   		
            <td><strong><?php echo $value['num1'] ?> x <?php echo $value['num2']; ?> </strong></td>
            <td><input type="text" name="q<?php echo $value['sno'];?>" required="required" value="<?php if(isset($answer)){echo $q1;} ?>"></td>

                <?php } ?>
           </tr>
<?php    } ?>

and the output is

5 x 5 = input field		5 x 5 = input field		5 x 5= input field 	
5 x 10 = input field		5 x 10 = input field		5 x 10 = input field	
10 x 4 = input field		10 x 4 = input field		10 x 4 = input field	

and so on … i.e each key array get repeat 3 times…

any help to print 3 key values per <tr> then next 3 in next <tr>

I have to make 10 rows like this i.e 30 <td>

You could use the [fphp]array_chunk[/fphp] function for that. It splits an array into smaller arrays containing the number of elements that you specify:


<?php foreach(array_chunk($fetch, 3) as $currentRow): ?>
    <tr>
        <?php foreach($currentRow as $key => $value): ?>
            <td><strong><?php echo $value['num1'] ?> x <?php echo $value['num2']; ?> </strong></td>
            <td><input type="text" name="q<?php echo $value['sno'];?>" required="required" value="<?php if (isset($answer)) echo $q1; ?>"></td>
        <?php endforeach; ?>
    </tr>
<?php endforeach; ?>

thanks for help . array_chunk() , this function is new to me…