alecsloman Posted April 27, 2010 Share Posted April 27, 2010 Hey, Not to abuse the good graces of the forum, but: I have an array in (menus.php), and after including that to (index.php), I echo some values with the following: foreach ($menu['main'] as $item) { echo "<li>$item</li>"; } That works fine, but the idea is that I want to automate this as much as possible. If I wanted to put that foreach statement into a function and place the function on (menus.php), how would I write it? And how would I call it in (index.php)? My goal is to be at a point where I can just write something like "printmenu($menu[main]); I keep looking at my book and various online tutorials, but...yeah, it just doesn't make sense. Regards, Alec Quote Link to comment Share on other sites More sharing options...
papaface Posted April 27, 2010 Share Posted April 27, 2010 function printarray($array) { foreach ($array as $item) { echo "<li>$item</li>"; } } //usage printarray($menu[main]); That'd do it, but im sure others will come up with better ways. Of course you could always just do: print_r($menu[main],true); But for that you'd need to have the elements include <li>*</li> Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 27, 2010 Share Posted April 27, 2010 Put this in menus.php file function printmenu($array) { foreach($array as $value) { echo "<li>$value</li>"; } } In index.php you would do this include('menus.php'); printmenu($menu[main]); Quote Link to comment Share on other sites More sharing options...
alecsloman Posted April 27, 2010 Author Share Posted April 27, 2010 See? That's why coders are the best people. Thanks dude(s), that worked perfectly. I understand the principle now, and I've got it working on my site. 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.