Jump to content

array within array


Jaswinder

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/288008-array-within-array/
Share on other sites

thanks for this nice function, array_chunk() .. its new to me

Did it in this way

  <?php 
		$fetch=$check->gettest("level1",$cn);
		$group=array_chunk($fetch,3);
		
	 foreach($group 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
		 } ?>
          </tr>
          <?php 
	 }
		
		?>
Link to comment
https://forums.phpfreaks.com/topic/288008-array-within-array/#findComment-1477260
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.