php - 输入类型复选框中的值数组?

标签 php html arrays

<form action="next.php" method="post">
<table>
    <tr>
      <td>Paul</td>
      <td>Attuck</td>
      <td>paulattuck@yahoo.com</td>
      <td><input type="checkbox" name="list[]" value="paulattuck@yahoo.com" /></td>
    </tr>
    <tr>
      <td>James</td>
      <td>Bond</td>
      <td>jamesbond@yahoo.com</td>
      <td><input type="checkbox" name="list[]" value="jamesbond@yahoo.com" /></td>
    </tr>
    <tr>
      <td>Name</td>
      <td>Last</td>
      <td>lastname@yahoo.com</td>
      <td><input type="checkbox" name="list[]" value="lastname@yahoo.com" /></td>
    </tr>
</table>
    <input type="submit" name="submit" value="submit" />
</form>

如果我选中所有复选框并将表单发送到 next.php 我可以:

print_r($_POST['tags']);
// output
Array
(
    [0] => paulattuck@yahoo.com
    [1] => jamesbond@yahoo.com
    [2] => lastname@yahoo.com
)

我怎么做:

Array
    (
        [0] => array ([0] => Paul
                      [1] => Attuck
                      [2] => paulattuck@yahoo.com)
        [1] => array ([0] => James
                      [1] => Bond
                      [2] => jamesbond@yahoo.com)
        [2] => array ([0] => Last
                      [1] => Name
                      [2] => lastname@yahoo.com)
    )

?我尝试使用序列化,但这使得“- 不能以 html 形式使用它。

最佳答案

如果你确定名称字段中有一个禁止使用的字符(我在这里使用|),你可以这样做

<tr>
      <td>Paul</td>
      <td>Attuck</td>
      <td>paulattuck@yahoo.com</td>
      <td><input type="checkbox" name="list[]" value="Paul|Attuck|paulattuck@yahoo.com" /></td>
</tr>

并且会得到

print_r($_POST['tags']);
// output
Array
(
    [0] => Paul|Attuck|paulattuck@yahoo.com
    [1] => James|Bond|jamesbond@yahoo.com
    [2] => Last|Name|lastname@yahoo.com
)

你可以通过哪些方式进行转换

$names=array();
foreach ($_POST['tags']) as $tag)
  $names[]=explode('|',$tag,3);

关于php - 输入类型复选框中的值数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8987873/

相关文章:

javascript - 通过单击任何单元格或表格数据及其左侧和顶部标题进行查找

python - NumPy 根据另一个数组中的值对第三个数组中的每个匹配元素求和一个数组

c - 在程序中得到错误的输出

html - 样式 ul 宽度以容纳元素

html - 无法更改 aside 标签 html5 中元素的背景

javascript - JQuery Ajax 文件上传在客户端浏览器上无法正常工作

php - 如何用 wamp 运行 Glassfish 服务器?

c - 结构数组中的值变成垃圾值

php - 如何将 PHP crypt 实现转换为 Ruby?

php - 获取站点访问者位置的最佳方法是什么?