如何在 PHP 中发布关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14050812/
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
How to POST an associative array in PHP
提问by user1134475
I have the following form:
我有以下表格:
<form action="options.php" method="post">
<input type="text" name="deptid" id="deptid" />
<input type="text" name="deptname" id="deptname" />
<input type="submit" name="submit" id="submit" value="save" />
</form>
EDIT
编辑
Is it possible to pass the two values into one associative array BEFOREsubmission ? I would like to pass it in this form:
是否可以在提交之前将这两个值传递到一个关联数组中?我想以这种形式传递它:
array('deptid'=>'deptname')
I need this because I avoid to modify the script of the destination php file(options.php)
我需要这个,因为我避免修改目标 php 文件(options.php)的脚本
Thanks.
谢谢。
回答by Jared Farrish
Here is a method using pure HTML that get's you nearlyexactly where you want to be, and only uses HTML:
这是一种使用纯 HTML 的方法,可以让您几乎准确地到达您想要的位置,并且只使用 HTML:
<form action="options.php" method="post">
<input type="text" name="options[deptid]" id="deptid" />
<input type="text" name="options[deptname]" id="deptname" />
<input type="submit" name="submit" id="submit" value="save" />
</form>
Which would give you in PHP:
这会给你在 PHP:
$post_options = array(
'options' => array(
'deptid '=> '[that input element value]',
'deptname' => '[that input element value]'
)
);
Which you can then (including sanitizing) access such as this:
然后您可以(包括消毒)访问,例如:
$post_options = array('options');
if (is_numeric($post_options['deptid'] && $post_options['deptid'] > 0) {
// Do whatever
}
if (is_string($post_options['deptname'] && strlen($post_options['deptname'] > 2)) {
// Do whatever
}
EDIT
编辑
Or... You want to reference the deptidin the input nameattribute and use it to modify the row for a department name? Which seems to indicate something like this:
或者......你要引用deptid的input name属性,并用它来修改行一个部门的名字吗?这似乎表明了这样的事情:
<?php
$deptid = 1;
$deptname = 'Department of Silly Walks';
?><input type="hidden" name="options[<?=$deptid?>]" value="<?=$deptname?>">
Which outputs:
哪些输出:
<input type="hidden" name="options[1]" value="Department of Silly Walks">
The problem with this is that the $deptidvalue becomes a value that's not actually directly named or referenced. I think this is potentially problematic to implement due to this abstraction of the value from the server to the client and back, so I would recommend what I have at the top instead. It's not much of a difference in practice, but it's more or less self-documenting.
这样做的问题是该$deptid值变成了一个实际上并未直接命名或引用的值。我认为由于从服务器到客户端和返回值的这种抽象,实现这可能有问题,所以我会推荐我在顶部拥有的东西。这在实践中没有太大区别,但它或多或少是自我记录的。
Note, if you wanted to serialize a list of departments, it's a little trickier. You might, for instance, try this:
请注意,如果您想序列化部门列表,则有点棘手。例如,你可以试试这个:
<input type="text" name="options[][deptid]" id="deptid" />
<input type="text" name="options[][deptname]" id="deptname" />
Which would add an indexed value for every input. However... They were would not be directly associated. So you would get, instead, two zero-indexed arrays for each key.
这将为每个input. 然而……他们不会有直接的关联。因此,您将获得每个键的两个零索引数组。
What I would suggest in this case is to use Javascript to add each new department's inputelements, so you can give each a number like:
在这种情况下,我建议使用 Javascript 添加每个新部门的input元素,因此您可以为每个部门指定一个数字,例如:
<input type="text" name="options[0][deptid]" id="deptid" />
<input type="text" name="options[0][deptname]" id="deptname" />
<br/>
<input type="text" name="options[1][deptid]" id="deptid" />
<input type="text" name="options[1][deptname]" id="deptname" />
<br/>
<input type="text" name="options[2][deptid]" id="deptid" />
<input type="text" name="options[2][deptname]" id="deptname" />
<br/>
<input type="text" name="options[3][deptid]" id="deptid" />
<input type="text" name="options[3][deptname]" id="deptname" />
Or do the old-school POSTBACK method and use PHP to count $POST['options']and "manually" add a new "row" of inputs with the same index. It's a common trap, so you just have to think about it if this is what you're after at some point.
或者使用老式的 POSTBACK 方法并使用 PHP 计算$POST['options']并“手动”添加具有相同索引的新“行”输入。这是一个常见的陷阱,所以你只需要考虑一下,如果这就是你在某个时候所追求的。
回答by Netorica
$_POSTis already an associative arrayand I recommend you not to complicate things beyond thatbecause $_POSTalready holds the data came from your form.
$_POST已经是一个关联数组,我建议您不要将其复杂化,因为$_POST已经保存了来自表单的数据。
$myassoc = $_POST;
print_r($myassoc);
and the associative array that you will receive is organized and named same in the name attribute of the input elements in your form (including textarea)
并且您将收到的关联数组在表单中输入元素的 name 属性中组织和命名相同(包括 textarea)
Other Insights
其他见解
As I see your code you want to put the deptnamedata to deptidas it reaches the PHP server-side code. well the thing you can do with is is just assign it to the key deptid
当我看到您的代码时,您希望将deptname数据放入deptidPHP 服务器端代码。那么你可以做的就是将它分配给键deptid
$_POST['deptid'] = $_POST['deptname'];
$myassoc = $_POST;
print_r($myassoc);
回答by Tahir Yasin
<form method="post">
<input type="text" name="formdata['deptid']" />
<input type="text" name="formdata['deptname']" />
<input type="submit" />
</form>
<?php
if(isset($_POST['formdata']))
{
$deptid = $_POST['formdata']['deptid'];
$deptname = $_POST['formdata']['deptname'];
}
?>
回答by bearfriend
Build a JS object with the appropriate structure, convert it to JSON with JSON.stringify(), POST it, and then do json_decode($_POST['data'],true).
使用适当的结构构建一个 JS 对象,使用 将其转换为 JSON JSON.stringify(),发布它,然后执行json_decode($_POST['data'],true).
You'll have an exact copy from JS object, to PHP associate array. Drop the second parameter of trueto get a PHP object.
您将拥有从 JS 对象到 PHP 关联数组的精确副本。删除第二个参数true以获取 PHP 对象。
回答by LoneWOLFs
$_POSTis already an associative array.
$_POST已经是一个关联数组。
You can rebuild an array of the form you need from this by just assigning $_POSTto a variable
您可以通过仅分配$_POST给变量来重建您需要的表单数组
$myarray = $_POST;
Now $myarray is what you require. Do var_dump($myvar);.
现在 $myarray 就是你所需要的。做var_dump($myvar);。
回答by David Harris
Why would you want to do that? But, you CAN send "arrays" through forms like this:
你为什么想这么做?但是,您可以通过以下形式发送“数组”:
<form method="post">
<input type="text" name="textboxes[]" />
<input type="text" name="textboxes[]" />
<input type="submit" />
</form>
<?php
if(isset($_POST['textboxes']))
var_dump($_POST['textboxes']);
?>
回答by Omiga
$deptid = $_POST['deptid'];
$array = array($$deptid => $_POST['deptname']);
print_r($array);

