如何从数组中取值并放入会话中

如何从数组中取值并放入会话中

问题描述:

Here i have one array from this array i want to take fullname & email, and put in to session variable,i think to use foreach loop the we have to put session variable i tried but i am not able to ? can you please anyone update my answer

print_r($user_data);

    Array
(
    [id] => 1
    [i_am] => Individual
    [fullName] => Kanniyappan
    [emailId] => kanniyappan@g2evolution.co.in
    [password] => welcome
    [mobileNo] => 9986128665
    [maxContactLeads] => 
    [otp] => 502649
    [otpstatus] => Active
    [createdOn] => 2017-10-14 20:24:18
    [regStatus] => Active
)

I tried

 foreach ($user_data as $ukey => $uvalue) {
                    # code...
                    echo $uvalue['mobileNo'];
                }

You can assign array values to session variables like this:

$_SESSION['fullName'] = $user_data['fullName'];
$_SESSION['emailId'] = $user_data['emailId'];

Well, I see what you are trying to do; When you run the foreach loop you are separating the $key and the $value; so you have to take a bit of different approach to your problem:

You could try simply assigning the $_SESSION values manually like follows:

$_SESSION['email'] = $user_data['emailId'];

This will equal your $_SESSION to your emailId key's value.

You can also run a foreach loop and get the key's and values:

foreach ($user_data as $key => $value) {
    $_SESSION[$key] = $value;
}

This will set each array value to a session variable. And your output will create a $_SESSION value for each value in your array; you can skip some values in your loop by doing a if queries and continue on values you do not want.

For example, run something like this inside your foreach loop; this will skip the value where the $key equal i_am:

if ( $key == 'i_am' )
{
    continue;
}