alecsloman Posted April 27, 2010 Share Posted April 27, 2010 Hi, I'm new to PHP, so go easy on me. I have file (menus.php) a multi-dimensional array where I'm storing menus/items. $menu = array ( "main" => array ( "Logging In/Out", "The Program Window", "My Settings" ), "basics" => array ( "Populating Records", "Deleting Records", "Saving Records" ), "sentinel" => array ( "Item 1", "Item 2", "Item 3", ) ); I want to print the items in another different file (like say index.html), all the items in a sub-arays using a foreach, like: foreach ($menu["main"] as $item) { echo "<li>$item</li><br />"; } But I'm not sure how to pass the values from the array to the html file. I guess actually it would be the html "getting" the values from the php file. I've looked for ways to do this, but most tutorials on this are high level. I'm not looking for a best practice, I am really trying to learn PHP ground up. Thanks everyone! -Alec Quote Link to comment https://forums.phpfreaks.com/topic/199940-new-to-php-getting-values-from-an-array-in-another-document/ Share on other sites More sharing options...
masterwebxz Posted April 27, 2010 Share Posted April 27, 2010 $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42)); echo $arr["somearray"][6]; // 5 echo $arr["somearray"][13]; // 9 echo $arr["somearray"]["a"]; // 42 This is a god example! Quote Link to comment https://forums.phpfreaks.com/topic/199940-new-to-php-getting-values-from-an-array-in-another-document/#findComment-1049421 Share on other sites More sharing options...
alecsloman Posted April 27, 2010 Author Share Posted April 27, 2010 Yes, I understand how to echo values from an array in general. What I am wondering is how to echo values from an array, with a foreach function, except the array is in a different file. Hopefully without using a POST? -Alec Quote Link to comment https://forums.phpfreaks.com/topic/199940-new-to-php-getting-values-from-an-array-in-another-document/#findComment-1049433 Share on other sites More sharing options...
masterwebxz Posted April 27, 2010 Share Posted April 27, 2010 foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement and 2 examples: <?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } // $arr is now array(2, 4, 6, unset($value); // break the reference with the last element ?> AND <?php /* foreach example 1: value only */ $a = array(1, 2, 3, 17); foreach ($a as $v) { echo "Current value of \$a: $v.\n"; } ?> OR $a = array(1, 2, 3, 17); $i = 0; /* for illustrative purposes only */ foreach ($a as $v) { echo "\$a[$i] => $v.\n"; $i++; } good luck! Quote Link to comment https://forums.phpfreaks.com/topic/199940-new-to-php-getting-values-from-an-array-in-another-document/#findComment-1049434 Share on other sites More sharing options...
mikesta707 Posted April 27, 2010 Share Posted April 27, 2010 web, he's talking about how to use one file's variables in another file if I'm not mistaken. use include, but you may want to consider making the array a constant if the values never (or rarely) change. include("menus.php"); print_r($menu); Quote Link to comment https://forums.phpfreaks.com/topic/199940-new-to-php-getting-values-from-an-array-in-another-document/#findComment-1049438 Share on other sites More sharing options...
OOP Posted April 27, 2010 Share Posted April 27, 2010 Hi there, you need to generate your HTML code using PHP for example, i will have a file called myHTML.php in the same folder of the menus.php which contains the following: include('menus.php'); // This is one way that shows how to include the contents of one file into another using PHP foreach ($menu["main"] as $item) { echo "<li>$item</li><br />"; } I hope that is clear enough to get you started regards Quote Link to comment https://forums.phpfreaks.com/topic/199940-new-to-php-getting-values-from-an-array-in-another-document/#findComment-1049440 Share on other sites More sharing options...
alecsloman Posted April 27, 2010 Author Share Posted April 27, 2010 Thanks everyone! Yes, that is exactly what I was looking for. This is a great forum. You guys responded really quickly, and gave great advice. Thanks to you all. -Alec Quote Link to comment https://forums.phpfreaks.com/topic/199940-new-to-php-getting-values-from-an-array-in-another-document/#findComment-1049443 Share on other sites More sharing options...
alecsloman Posted April 27, 2010 Author Share Posted April 27, 2010 web, he's talking about how to use one file's variables in another file if I'm not mistaken. use include, but you may want to consider making the array a constant if the values never (or rarely) change. include("menus.php"); print_r($menu); If my array looks like this: $menu = array ( "main" => array ( "Logging In/Out", "The Program Window", "My Settings", "Viewing System Information", "Help Menu & Context Sensitive Help", "Data Records", "Data Record Tabs", "Tab Grids", ), "basics" => array ( "Populating Records", "Deleting Records", "Saving Records" ), "sentinel" => array ( "Item 1", "Item 2", "Item 3", ) ); ...how does it need to change to be a constant? And yes, the values should never change, so I suppose it's just safer to make it a constant so nothing happens accidentally later? Thanks, Alec Quote Link to comment https://forums.phpfreaks.com/topic/199940-new-to-php-getting-values-from-an-array-in-another-document/#findComment-1049447 Share on other sites More sharing options...
mikesta707 Posted April 27, 2010 Share Posted April 27, 2010 actually I kind of jumped the gun when I mentioned constants. Only scalar (like ints, floats, strings, etc.) can be made into costants. There are some workarounds, like storing the serialized string of the array into a constant, but honestly, I wouldn't bother, as its not that big of a deal Quote Link to comment https://forums.phpfreaks.com/topic/199940-new-to-php-getting-values-from-an-array-in-another-document/#findComment-1049461 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.