0

I am pulling data from database and would like to save it into session variables. I would like to name the keys the same that my table cells are named. So for example: I have a cell named "EMAIL", and I would like to get $_SESSION["EMAIL"]

I already have the data from database saved in an array ($data), which has array keys named after the cells, but I would like to move that data to SESSION array, with same keys...

How can I do this dynamically?

2 Answers 2

2

You could either do it like this:

foreach($myArr as $k=>$v) {
  $_SESSION[$k] = $v;
}

Or,

$_SESSION['user'] = $myArr;

In the first case, you will access email by doing $_SESSION['EMAIL'], and in the second case, $_SESSION['user']['EMAIL'];

Sign up to request clarification or add additional context in comments.

Comments

1

Another alternative is

$_SESSION = array_merge($_SESSION, $myArr);

I'm not sure if this is a good practice though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.