Can I put associative arrays in form inputs to be processed in PHP?(我可以将关联数组放在表单输入中以在 PHP 中处理吗?)
问题描述
我知道我可以做<input name="foo[]">之类的事情,但是否可以做<input name="foo[bar]"> 并让它在 PHP 中显示为 $_POST['foo']['bar']?
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']?
我问的原因是因为我正在制作一个巨大的表单元素表(包括带有多个选择的 ),并且我希望我的数据组织得井井有条我要发布的脚本.我希望每列中的输入元素具有相同的基本名称,但作为数组键的行标识符不同.这有意义吗?
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?
我已经完全尝试过了,但显然 Drupal 正在干扰我正在尝试做的事情.我以为我只是弄错了语法.Firebug 告诉我,我的输入名称的构造完全如此,但我的数据返回为 [foo[bar]] =>data 而不是 [foo] =>数组([bar] => 数据).
 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]] => data rather than [foo] => array([bar] => data).
编辑 2: 看来我真正的问题是我假设 Drupal 中的 $form_state['values'] 与 $_POST 具有相同的数组层次结构代码>.我不应该认为 Drupal 会如此合理和直观.我很抱歉浪费你的时间.你可以继续你的事业.
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.
推荐答案
您也可以在 Drupal 中轻松完成此操作.您必须记住的重要一点是将表单 '#tree' 参数设置为 TRUE.给你一个简单的例子:
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:
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;
}
现在,如果你在 MYMODULE_form_submit($form, &$form_state) 中 print_r() $form_state['values'],你会看到这样的:
Now, if you print_r() $form_state['values'] in MYMODULE_form_submit($form, &$form_state), you will see something like this:
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
)
                        这篇关于我可以将关联数组放在表单输入中以在 PHP 中处理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以将关联数组放在表单输入中以在 PHP 中处理吗?
				
        
 
            
        基础教程推荐
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
 - 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
 - Yii2 - 在运行时设置邮件传输参数 2022-01-01
 - php中的foreach复选框POST 2021-01-01
 - php中的PDF导出 2022-01-01
 - Web 服务器如何处理请求? 2021-01-01
 - PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
 - php 7.4 在写入变量中的 Twig 问题 2022-01-01
 - 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
 - 将变量从树枝传递给 js 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				