Jaswinder Posted April 25, 2014 Share Posted April 25, 2014 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 More sharing options...
davidannis Posted April 25, 2014 Share Posted April 25, 2014 You don't need the for $i. The for each will loop through your array. Just put a counter in $x++; and then if ($x%3==0) {echo '</table><table>';} Link to comment https://forums.phpfreaks.com/topic/288008-array-within-array/#findComment-1477252 Share on other sites More sharing options...
cyberRobot Posted April 25, 2014 Share Posted April 25, 2014 Alternatively, you could use array_chunk() to break apart the columns. http://www.cyberscorpion.com/2013-08/build-html-tables-dynamically-with-php-part-2-simplify-with-array_chunk/ Link to comment https://forums.phpfreaks.com/topic/288008-array-within-array/#findComment-1477253 Share on other sites More sharing options...
Jaswinder Posted April 25, 2014 Author Share Posted April 25, 2014 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.