tutorialstuff Posted February 29, 2008 Share Posted February 29, 2008 I've written a function that creates uses an array over and over so if I want to use that same array in different context such as a list or for a drop down menu all I have to do is call the function and declare an open tag and closing tag, but sometimes I might want to use a value with that function that prints out the array name but I'm having a hard time figuring out how. Maybe somebody can help. You can see a live demo at: http://tutorialstuff.com/test/arrays.php and the code is below. Thanks - Mike <?php function rooms($open, $close){ $rooms = array('Bedroom','Living/Family Room','Dinning Room','Kitchen','Utility Room','Garage'); $i = 0; while($rooms[$i]){ print "$open$rooms[$i]$close"; $i++; } } print rooms("<b>", "</b><br />\n"); ?> <form> <select> <?php print rooms("<option value=\"$rooms[$i]\">", "</option>\n"); ?> </select> </form> Quote Link to comment Share on other sites More sharing options...
fnairb Posted February 29, 2008 Share Posted February 29, 2008 For your first example you are probably better off just using an implode. <?php $rooms = array('Bedroom','Living/Family Room','Dinning Room','Kitchen','Utility Room','Garage'); echo "<b>", implode("</b><br /><b>\n", $rooms), "</b><br />\n"; // or you could use print print "<b>" . implode("</b><br /><b>\n", $rooms) . "</b><br />\n"; ?> If I am understanding the second one you are really just using the value twice. First off increase the reuse of it pass in the array. Then pass the wrapping text in as array pieces. This will take an arbitrary number of values and insert it between each element of the wrapping pieces. (This will work for the <b> example as well) <?php $rooms = array('Bedroom','Living/Family Room','Dinning Room','Kitchen','Utility Room','Garage'); function wrapem($what, $enclosure) { foreach ($what as $val) { $final .= implode($val, $enclosure); } return $final; } ?> <form> <select> <?php print wrapem($rooms, array('<option value="', '">', "</option>\n")); ?> </select> </form> Quote Link to comment Share on other sites More sharing options...
fnairb Posted February 29, 2008 Share Posted February 29, 2008 Lets try that again..... For your first example you are probably better off just using an implode. <?php $rooms = array('Bedroom','Living/Family Room','Dinning Room','Kitchen','Utility Room','Garage'); echo "<b>", implode("</b><b>\n", $rooms), "</b>\n"; // or you could use print print "<b>" . implode("</b><b>\n", $rooms) . "</b>\n"; ?> If I am understanding the second one you are really just using the value twice. First off increase the reuse of it pass in the array. Then pass the wrapping text in as array pieces. This will take an arbitrary number of values and insert it between each element of the wrapping pieces. (This will work for the example as well) <?php $rooms = array('Bedroom','Living/Family Room','Dinning Room','Kitchen','Utility Room','Garage'); function wrapem($what, $enclosure) { foreach ($what as $val) { $final .= implode($val, $enclosure); } return $final; } ?> <form> <select> <?php print wrapem($rooms, array('<option value="', '">', "</option>")); ?> </select> </form> Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 29, 2008 Share Posted February 29, 2008 fyi you can edit posts fnairb Quote Link to comment Share on other sites More sharing options...
fnairb Posted February 29, 2008 Share Posted February 29, 2008 Yes. I had an ID-10-T error and didn't notice that the browser window I had opened wasn't logged in to the site, freaked out that the edit option wasn't available. :-\ Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 29, 2008 Share Posted February 29, 2008 lol ul 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.