Reputation: 2723
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?
Upvotes: 0
Views: 94
Reputation: 787
Another alternative is
$_SESSION = array_merge($_SESSION, $myArr);
I'm not sure if this is a good practice though.
Upvotes: 1
Reputation: 42450
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']
;
Upvotes: 2