lilmer Posted February 14, 2013 Share Posted February 14, 2013 <?php $test_ar = array('a','b','c','d','e'); for($i=1;$i<=10;$i++){ foreach($test_ar as $count){ echo $i.'-'.$count.'<br>'; } } so the output will be. 1-a 1-b 1-c 1-d 1-e 2-a 2-b etc. . . So how can I make this an output that will generate but using this for loop and foreach? 1-a-b-c-d-e 2-a-b-c-d-e 3-a-b-c-d-e etc. ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2013 Share Posted February 14, 2013 Echo the $i before the foreach... Quote Link to comment Share on other sites More sharing options...
AviNahum Posted February 14, 2013 Share Posted February 14, 2013 $test_ar = array('a','b','c','d','e'); for($i=1;$i<=10;$i++){ $output = $i; foreach($test_ar as $count){ $output .= " $count-"; } $output .= "<br>"; echo $output; } Quote Link to comment Share on other sites More sharing options...
lilmer Posted February 14, 2013 Author Share Posted February 14, 2013 Thanks guys. . but again the output is going outside the table. . ! noob me! Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted February 14, 2013 Share Posted February 14, 2013 (edited) If you're putting it in a table, put each bit of data in it's own <td> tag, for example test this: <?php $test_ar = array('a','b','c','d','e'); $output = "<table>"; for($i=1;$i<=10;$i++){ $output .= "<tr><td>"; $output .= $i; $output .= "</td>"; foreach($test_ar as $count){ $output .= "<td> $count-</td>"; } $output .= "</tr>"; } $output .= "</table>"; ?> <html> <head> <style type="text/css"> table,tr,td{border: 1px solid black;} </style> </head> <body> <?php print($output); ?> </body> </html> Regards, L2c. Edited February 14, 2013 by Love2c0de Quote Link to comment Share on other sites More sharing options...
lilmer Posted February 14, 2013 Author Share Posted February 14, 2013 Again by the way how do I resolve this?? <table> <tr> <?php $test_ar = array('a','b','c','d','e'); for($i=1;$i<=5;$i++){ echo '<td>'.$i; foreach($test_ar as $count){ echo ''.$count.''; } echo '-</td>'; } ?> </tr> </table> The output will be: 1abcde- 2abcde- 3abcde- 4abcde- 5abcde- 6abcde- 7abcde- 8abcde- 9abcde- 10abcde- How to make it like this: 1a - 2b - 3c - 4d -5e Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2013 Share Posted February 14, 2013 $test_ar = array('a','b','c','d','e'); $output = array(); foreach($test_ar AS $k=>$v){ $output[] = $k.$v; } echo implode(" - ", $output); Quote Link to comment Share on other sites More sharing options...
lilmer Posted February 14, 2013 Author Share Posted February 14, 2013 is there no solution about outputting my array inside a for loop? Cause I'm using a library and I didn't try to change the output so I just made a function array and just include it inside the loop. I know I understand that everytime my foreach has a value it will generate it again and again. How can I break it to get the first letter then on the next loop it's the second letter again. . or maybe some idea for that. THanks by the way! Quote Link to comment Share on other sites More sharing options...
AviNahum Posted February 14, 2013 Share Posted February 14, 2013 just tell us what you trying to reach :| Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2013 Share Posted February 14, 2013 First off, You need to be specific about what you want. Every time you ask we've given the code then you come back and say you want something different. the code I wrote for you does EXACTLY what you asked for. Ask for EXACTLY what you need, provide the code you have, and we can help. Otherwise, this is a waste of everyone's time and it's pissing me off. Quote Link to comment Share on other sites More sharing options...
lilmer Posted February 14, 2013 Author Share Posted February 14, 2013 Okay, thanks guys. Maybe I just gonna create new topic for what I really want. BTW Happy Valentines day! Quote Link to comment Share on other sites More sharing options...
lilmer Posted February 14, 2013 Author Share Posted February 14, 2013 Okay, thanks guys. Maybe I just gonna create new topic for what I really want. BTW Happy Valentines day! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.