简体   繁体   English

PHP的会话foreach存储数据问题

[英]php session foreach storing data issue

I have a form that submits data. 我有一个提交数据的表格。 Let's for example say it submits this data: 例如,假设它提交了以下数据:

    Age     => 23
    Gender  => Male 
    Country => UK

Now I need to store this in a session array, that I have already made. 现在,我需要将其存储在已经完成的会话数组中。 BUT, this array already exists AND contains more fields than are given by the first form. 但是,此数组已存在,并且包含的​​字段比第一种形式提供的字段更多。 For example, this is what the session array could look like: 例如,会话数组如下所示:

    Age => Value
    Gender => Value
    Country => Value
    State => Value
    Language => Value

As you can see, only the first 3 are given with the first form. 如您所见,第一个表格只给出前三个。 I would like a for each loop that detects which values are given (in this case: age, gender and country) and then place those values within the session array. 我想要一个for每个循环,用于检测给出的值(在这种情况下:年龄,性别和国家/地区),然后将这些值放入会话数组中。

For example, the next form would give the information: 例如,下一个表格将提供以下信息:

    State => Value
    Language => Value

I've been trying to wrap my head around this, but I just can't find a solution.. :/ 我一直在努力解决这个问题,但是我找不到解决方法..:/

Something like: 就像是:

$valid_fields = array('Age', 'Gender', 'Country', 'State', 'Language');
foreach ($_POST as $key => $value) {
    if (in_array($key, $valid_fields)) {
        $_SESSION[$key] = $value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM