PHP $_POST 获取数据数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3148743/
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-25 08:51:32  来源:igfitidea点击:

PHP $_POST get data array

php

提问by Jorge

I'm trying to do a multiple textbox with the same names.
Here is my code.

我正在尝试使用相同的名称创建多个文本框。
这是我的代码。

HTML

Email 1:<input name="email" type="text"><br>
Email 2:<input name="email" type="text"><br>
Email 3:<input name="email" type="text"><br>


PHP

$email = $_POST['email'];
echo $email;

I wanted to have a results like this:

[email protected], [email protected], [email protected]

How can I do that? is that possible?

我想要这样的结果:

[email protected][email protected][email protected]

我该怎么做?那可能吗?

回答by Pekka

Using []in the element name

使用[]的元素名称

Email 1:<input name="email[]" type="text"><br>
Email 2:<input name="email[]" type="text"><br>
Email 3:<input name="email[]" type="text"><br>

will return an array on the PHP end:

将在 PHP 端返回一个数组:

$email = $_POST['email'];   

you can implode()that to get the result you want:

你可以implode()得到你想要的结果:

echo implode(", ", $email); // Will output [email protected], [email protected] ...

Don't forget to sanitize these values before doing anything with them, e.g. serializing the array or inserting them into a database! Just because they're in an array doesn't mean they are safe.

在对这些值做任何事情之前不要忘记清理它们,例如序列化数组或将它们插入到数据库中!仅仅因为它们在一个数组中并不意味着它们是安全的。

回答by Piskvor left the building

<input name="email[]" type="text">
<input name="email[]" type="text">
<input name="email[]" type="text">
<input name="email[]" type="text">

$_POST['email'] will be an array.

$_POST['email'] 将是一个数组。

回答by Alp Altunel

Another example could be:

另一个例子可能是:

<input type="text" name="email[]" value="1">
<input type="text" name="email[]" value="2">
<input type="text" name="email[]" value="3">

<?php
     foreach($_REQUEST['email'] as $key => $value)
          echo "key $key is $value <br>";

will display

会显示

key 0 is 1
key 1 is 2
key 2 is 3