【发布时间】: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你应该明确你需要什么。 -
它们不是随机的,不幸的是,这个问题相当糟糕。