shergold Posted August 25, 2009 Share Posted August 25, 2009 Hey guys, i was wondering if anyone could help me, ive tried echoing the array but i get no output and it does not display Array. Thanks allot, shergold. <?php //Test Array $tiles; $tiles["01,01"] = ""; $tiles["02,01"] = ""; $tiles["03,01"] = ""; function draw_game() { echo "<table border=\"0\" width=\"500\" height=\"500\" />"; //for each of the tiles execute the code print_r($tiles); foreach( $tiles as $id => $tile) { //add start of table row tag if count is equal to 10,20,30 if ($count == 0 || $count == 10 || $count == 20 || $count == 30) { echo "<tr>"; } echo "<td width=\"50\" height=\"50\" alight=\"left\" />"; //check what tile image to output, default is grass switch($id) { case 1: echo "<img src=\"$id.gif\" alt=\"$tile\" />"; break; default: echo "<img src=\"grass.gif\" alt=\"$tile\" />"; } echo "</td>"; //add end of table row tag if count is equal to 10,20,30 if ($count == 0 || $count == 10 || $count == 20 || $count == 30) { echo "<tr>"; } $count++; } echo "</table>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171839-solved-invalid-argument-supplied-for-foreach-please-help/ Share on other sites More sharing options...
kickstart Posted August 25, 2009 Share Posted August 25, 2009 Hi The array is declared outside the function. Unless you have it declared as global within the function or passed to the function then nothing inside the function knows about it. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/171839-solved-invalid-argument-supplied-for-foreach-please-help/#findComment-906078 Share on other sites More sharing options...
shergold Posted August 25, 2009 Author Share Posted August 25, 2009 ah thankyou very much, i didn't think. Thanks again, shergold. Quote Link to comment https://forums.phpfreaks.com/topic/171839-solved-invalid-argument-supplied-for-foreach-please-help/#findComment-906080 Share on other sites More sharing options...
AviNahum Posted August 25, 2009 Share Posted August 25, 2009 you set the $tiles outside the function and try to use it into a function? functions using only local vars (into the function) or you just make it global var... <?php //Test Array $tiles = array(); $tiles["01,01"] = ""; $tiles["02,01"] = ""; $tiles["03,01"] = ""; function draw_game() { global $tiles; echo "<table border="0" width="500" height="500" />"; //for each of the tiles execute the code print_r($tiles); foreach( $tiles as $id => $tile) { //add start of table row tag if count is equal to 10,20,30 if ($count == 0 || $count == 10 || $count == 20 || $count == 30) { echo "<tr>"; } echo "<td width="50" height="50" alight="left" />"; //check what tile image to output, default is grass switch($id) { case 1: echo "<img src="$id.gif" alt="$tile" />"; break; default: echo "<img src="grass.gif" alt="$tile" />"; } echo "</td>"; //add end of table row tag if count is equal to 10,20,30 if ($count == 0 || $count == 10 || $count == 20 || $count == 30) { echo "<tr>"; } $count++; } echo "</table>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171839-solved-invalid-argument-supplied-for-foreach-please-help/#findComment-906081 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.