【问题标题】:Loop through list of form elements array to get or fetch the elements we need or want to keep遍历表单元素数组列表以获取或获取我们需要或想要保留的元素
【发布时间】:2017-08-23 13:02:51
【问题描述】:

这是我当前的代码,我正在寻找一种更有效的编写方式。

需要使用 foreach 循环遍历每个变量或以某种方式将它们全部添加到数组中,而无需我重新编写每个变量名称。

$formValues = $form_state->getValues();    

$relocation = $formValues['relocation'];
$europe = $formValues['europe'];
$twoyears = $formValues['twoyears'];
$realestate = $formValues['realestate'];
$nominated = $formValues['check_nominated_by'];
$nom_comp = $formValues['nom_company'];
$nom_contact = $formValues['nom_contact'];
$nom_email = $formValues['nom_email'];
$contact1 = $formValues['contact1'];
$position1 = $formValues['contact_position1'];
$email1 = $formValues['email1'];
$contact2 = $formValues['contact2'];
$position2 = $formValues['contact2'];
$email2 = $formValues['contact2'];
$contact3 = $formValues['contact3'];
$position3 = $formValues['contact3'];
$email3 = $formValues['contact3'];


tempstore = array();
$tempstore['relocation'] = $relocation;
$tempstore['europe'] = $europe;
$tempstore['twoyears'] = $twoyears;
$tempstore['realestate'] = $realestate;
$tempstore['membertype'] = $membertype;
$tempstore['nominated_by'] = '';
// All other fields need to be in this array too
// But there are a lot of unwanted fields in the $formValues
$_SESSION['sessionName']['formName'] = $tempstore;

【问题讨论】:

  • 需要使用 foreach 循环遍历每个变量 - 有你的答案还是你只是想要它的代码?
  • $_SESSION['sessionName']['formName'] = $formValues 怎么样?
  • 使用带有您想要保留的键的数组遍历它们。
  • // All other fields need to be in this array too 你应该明确你需要什么。
  • 它们不是随机的,不幸的是,这个问题相当糟糕。

标签: php arrays loops foreach


【解决方案1】:

既然您知道要保留的密钥,您可以执行以下操作:

<?php

/** The keys you want to keep... **/
$keys_to_keep = [

];

/** Will be used to store values for saving to $_SESSION. **/
$temp_array = [];

/** Loop through the keys/values. **/
foreach ($formValues as $key => $value) {
    /** The correct key i.e. the key you'd like to save. **/
    if (in_array($key, $keys_to_keep)) {
        /** What you wish to do... **/
        $temp_array[$key] = $value;
    }
}


$_SESSION['sessionName']['formName'] = $temp_array;
?>

正在发生的事情是,您正在循环遍历您的 $formValues 并获取 both 数组中每一对的键和值。

然后将对您的$keys_to_keep 进行检查,以查看当前元素是否是您希望保留的元素,如果是,则将其保存到$temp_array

阅读材料

foreach

in_array

【讨论】:

  • @PascalClaes 请通读阅读材料,以后从一开始就清楚地澄清您的问题。
  • 接受计时器仍在运行。我知道 foreach 和 in_array 是如何工作的,这只是我自己从未想过的事情......
  • 您甚至不需要foreachin_array() 和所有其他东西。 $t = $formValues; unset($t['key1'], $t['key2']/*, and so on */); $_SESSION['sessionName']['formName'] = $t; unset($t);。它更短更清晰。
  • 除非 $formValues 中有 50 个不需要的键?
【解决方案2】:

您可以使用可变变量和 foreach。

Foreach($formValues as $key => $var){
    $$key = $var;
}

Echo $relocation ."\n" . $europe;

https://3v4l.org/QeLjp

编辑我现在看到您的数组变量键并不总是与您想要的变量名称相同。 在这种情况下,您不能使用上述方法。
在这种情况下,您需要使用 list() = array.

List($relocation, $europe) = $formValues;
// The list variables have to be in correct order I just took the first two for demo purpose.

【讨论】:

  • extract() 就是这样做的。但这对 OP 有何帮助?
  • @jeroen 不知道提取。但是,是的,这是真的。查看我的编辑,我没有看到变量与数组项不匹配。
  • extract() 并没有真正帮助我。变量名无关紧要
猜你喜欢
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
相关资源
最近更新 更多