Foreach和$ _SESSION不起作用

时间:2016-03-29 14:45:50

标签: php session foreach

我有一个创建新会话的功能:

public function Create($data = array())
{
    foreach($data as $name => $value)
    {
        return $_SESSION[$name] = $value;;
    }
}

我正在使用以下示例:

Session::Create([
    '_USER_ID' => $fetch_id,
    '_USER_IP' => $_SERVER['REMOTE_ADDR'],
    '_USER_AGENT' => $_SERVER['HTTP_USER_AGENT'],
    '_USER_ACCEPT_LANGUAGE' => $_SERVER['HTTP_USER_ACCEPT_LANGUAGE'],
    '_USER_ACCEPT_ENCODING' => $_SERVER['HTTP_USER_ACCEPT_ENCODING']
]);

这只创建一个会话“_USER_ID”,其他会话不存在。 哪里有问题?

var_dump: array(1) {["_USER_ID"]=> int(1)}

1 个答案:

答案 0 :(得分:1)

foreach结束后

return(仅供参考:return打破循环):

public function Create($data = array())
{
    foreach($data as $name => $value)
    {
        $_SESSION[$name] = $value;;
    }

    return $_SESSION;
}