gibbo1715 Posted December 1, 2009 Share Posted December 1, 2009 All My last question on sessions has got me thinking about case statements whick i believe are called switch statements in php i have an array which contains a set of 1 s and 0 s as follows 0,1,0,0,1 What i want to do is run some code if its a 1 (true or false if you prefer) I was kindly shown how to do this with an if statement as follows session_start(); if(isset($_SESSION['permissions'])) $menus = array( 'menu1', '<a href="http://www.google.co.uk">Menu2</a>' ,'menu3', 'menu4', 'menu5'); for($x = 0; $x < count($_SESSION['permissions']); $x++) { if($_SESSION['permissions'][$x]) { // display the menu item print $menus[$x]."<br />"; } } Is there a way to do the same thing with a switch statement? thanks gibbo Link to comment https://forums.phpfreaks.com/topic/183589-switch-statement-and-arrays/ Share on other sites More sharing options...
premiso Posted December 1, 2009 Share Posted December 1, 2009 Sure: for($x = 0; $x < count($_SESSION['permissions']); $x++) { switch ($_SESSION['permissions'][$x]) { default: case 1:// display the menu item print $menus[$x]."<br />"; break; case 0: // other code here } } I believe that is what you were getting after? Link to comment https://forums.phpfreaks.com/topic/183589-switch-statement-and-arrays/#findComment-969031 Share on other sites More sharing options...
gibbo1715 Posted December 1, 2009 Author Share Posted December 1, 2009 Thanks that answers that Gibbo Link to comment https://forums.phpfreaks.com/topic/183589-switch-statement-and-arrays/#findComment-969034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.