mwstewart Posted June 12, 2007 Share Posted June 12, 2007 Hello guys & girls, As part of my user authentication class I've written a method that sets session variables. I pass an array to the function by reference which contains elements populated with information from the users table in the DB (name, street, country etc) At the moment the function looks like this: /* Set the session variables for easy access to user data */ function _setSessionData(&$userData) { $_SESSION['loggedOn'] = true; // Logged on $_SESSION['userID'] = $userData['UserID']; $_SESSION['username'] = $userData['UserName']; $_SESSION['password'] = $userData['Password']; $_SESSION['firstName'] = $userData['FirstName']; $_SESSION['surname'] = $userData['Surname']; $_SESSION['email'] = $userData['Email']; $_SESSION['street'] = $userData['Street']; $_SESSION['city'] = $userData['City']; return true; } What I'm wondering is if it is possible to loop through the $userData array, and dynamically name and populate the session variables rather than listing them all and assigning like I have? Cheers, Mark Quote Link to comment https://forums.phpfreaks.com/topic/55280-solved-loop-through-array-and-create-variable-session-variable-names/ Share on other sites More sharing options...
kenrbnsn Posted June 12, 2007 Share Posted June 12, 2007 Yes, use a foreach loop: <?php foreach($userData as $key => $value) $_SESSION[$key] = $value; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/55280-solved-loop-through-array-and-create-variable-session-variable-names/#findComment-273231 Share on other sites More sharing options...
mwstewart Posted June 12, 2007 Author Share Posted June 12, 2007 Ken, that's great. Thank you. Mark Quote Link to comment https://forums.phpfreaks.com/topic/55280-solved-loop-through-array-and-create-variable-session-variable-names/#findComment-273242 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.