我可以将关联数组放入要在 PHP 中处理的表单输入中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6469063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:28:17  来源:igfitidea点击:

Can I put associative arrays in form inputs to be processed in PHP?

phphtmlarraysdrupalforms

提问by DLH

I know I can do things like <input name="foo[]">, but is it possible to do things like <input name="foo[bar]">and have it show up in PHP as $_POST['foo']['bar']?

我知道我可以做这样的事情<input name="foo[]">,但是有可能做这样的事情<input name="foo[bar]">并且让它在 PHP 中显示为$_POST['foo']['bar']

The reason I ask is because I'm making a huge table of form elements (including <select>with multiple selections), and I want to have my data organized cleanly for the script that I'm POSTing to. I want the input elements in each column to have the same base name, but a different row identifier as an array key. Does that make sense?

我问的原因是因为我正在制作一个巨大的表单元素表(包括<select>多个选择),并且我希望为我要发布的脚本干净地组织我的数据。我希望每一列中的输入元素具有相同的基本名称,但作为数组键的行标识符不同。那有意义吗?

EDIT:I tried exactly this already, but apparently Drupal is interfering with what I'm trying to do. I thought I was just getting my syntax wrong. Firebug tells me that my input names are constructed exactly like this, but my data comes back as [foo[bar]] => datarather than [foo] => array([bar] => data).

编辑:我已经完全尝试过了,但显然 Drupal 正在干扰我正在尝试做的事情。我以为我只是弄错了语法。萤火告诉我,我的名字输入构造完全一样,但我的数据回来为[foo[bar]] => data,而不是[foo] => array([bar] => data)

EDIT 2:It seems my real problem was my assumption that $form_state['values']in Drupal would have the same array hierarchy as $_POST. I should never have assumed that Drupal would be that reasonable and intuitive. I apologize for wasting your time. You may go about your business.

编辑 2:看来我真正的问题是我假设$form_state['values']在 Drupal 中将具有与$_POST. 我不应该认为 Drupal 会如此合理和直观。我很抱歉浪费你的时间。你可以去做你的事了。

采纳答案by Maciej Zgadzaj

You can do this in Drupal too, quite easily. The important thing you have to remember about is setting form '#tree' parameter to TRUE. To give you a quick example:

您也可以在 Drupal 中轻松完成此操作。您必须记住的重要一点是将表单 '#tree' 参数设置为 TRUE。给你一个简单的例子:

function MYMODULE_form() {
  $form = array('#tree' => TRUE);
  $form['group_1']['field_1'] = array(
    '#type' => 'textfield',
    '#title' => 'Field 1',
  );
  $form['group_1']['field_2'] = array(
    '#type' => 'textfield',
    '#title' => 'Field 2',
  );
  $form['group_2']['field_3'] = array(
    '#type' => 'textfield',
    '#title' => 'Field 3',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

Now, if you print_r() $form_state['values'] in MYMODULE_form_submit($form, &$form_state), you will see something like this:

现在,如果您在 MYMODULE_form_submit($form, &$form_state) 中使用 print_r() $form_state['values'],您将看到如下内容:

Array
(
    [group_1] => Array
        (
            [field_1] => abcd
            [field_2] => efgh
        )

    [group_2] => Array
        (
            [field_3] => ijkl
        )

    [op] => Submit
    [submit] => Submit
    [form_build_id] => form-7a870f2ffdd231d9f76f033f4863648d
    [form_id] => test_form
)

回答by Ernest Boabramah

Let say we want to print student scores using the form below:

假设我们要使用以下表格打印学生分数:

<form action="" method="POST">
  <input name="student['john']">
  <input name="student['kofi']">
  <input name="student['kwame']">
  <input type="submit" name="submit">
</form>

and PHP code to print their scores:

和 PHP 代码来打印他们的分数:

if(isset($_POST['submit']))
{
    echo $_POST['student']['john'] . '<br />';
    echo $_POST['student']['kofi'] . '<br />';
    echo $_POST['student']['kwame'] . '<br />'; 
}

This will print the values you input into the field.

这将打印您输入到字段中的值。

回答by Naftali aka Neal

Yes you can. you can even do name="foor[bar][]"and on for even more padding.

是的你可以。你甚至可以做name="foor[bar][]"更多的填充。