【问题标题】:Array of multiple select inputs多个选择输入的数组
【发布时间】:2014-02-15 09:51:13
【问题描述】:

我有这个表格:

<form>
    <select name="data[][]" multiple="multiple">
        <option>1</option>
        <option selected="selected">2</option>
    </select>

    <select name="data[][]" multiple="multiple">
        <option selected="selected">3</option>
        <option>4</option>
    </select>

    <select name="data[][]" multiple="multiple">
        <option selected="selected">5</option>
        <option selected="selected">6</option>
    </select>
</form>

我想要这个:

$_POST['data'] = array(
    0 => array(2),
    1 => array(3),
    2 => array(5,6)
);

我得到了这个:

$_POST['data'] = array(
    0 => array(2),
    1 => array(3),
    2 => array(5),
    3 => array(6)
);

解决方案是设置索引:name="data[0][]" 但我希望它自动完成..

有什么想法吗?

【问题讨论】:

  • 你是如何生成表单的?
  • 如果您不想一开始就设置索引,就不能指望它会神奇地出现…… PHP 不“知道”这是三个单独的选择框,它只看到它被传递的参数——如果这些都被命名为data[][],那么你就不能期待其他结果了。
  • 我认为 html 足够聪明,可以将每个多项选择分组到一个唯一索引中,但正如您所说,它是在 php 中完成的,所以我必须正确管理索引。谢谢!

标签: php arrays forms select option


【解决方案1】:

一种方式。更改为:

<select name="data[0][]" multiple="multiple"> 
 ...
<select name="data[1][]" multiple="multiple">
 ...
<select name="data[2][]" multiple="multiple">

另见@CBroe 评论。

【讨论】:

  • @Yuukuari 为什么?有什么问题?
  • 因为我想在javascript中动态添加这些多个选择输入。我构建了一个通用的多重选择,每次单击“添加”按钮时都会克隆它。相反,在您和 CBroe 所说的之后,我看到我必须使用带有索引的函数作为构建多选输入的参数。
  • 你可以动态地做到这一点。从零值开始的 Inc 索引(例如 index++)并设置,单击“添加”,将每个新的多重选择命名为 "data["+index+"][]" by js/jQuery
  • 我就是这么做的 :) 谢谢
【解决方案2】:
You don't need to insert the keys manually.
I tested your code and it seems to be correct for what you intend to get or you  expect.
I tested it like this;
<html>
<body>
<?php
if (!isset($_GET["submit"])) {
?>
<form method="get" action="yourfilename.php">
<select name="data[][]" multiple="multiple">
    <option>1</option>
    <option selected="selected">2</option>
</select>

<select name="data[][]" multiple="multiple">
    <option selected="selected">3</option>
    <option>4</option>
</select>

<select name="data[][]" multiple="multiple">
    <option selected="selected">5</option>
    <option selected="selected">6</option>
</select>
<input type="submit" value="submit" name="submit"/>
</form>
<?php
}
else {
$data= $_GET["data"];
print_r($data);
echo $show;
}

\\ So to get the 5 or 6 value in the array with key "2";
\\ Output: 5
$show= $data[2][0];

\\ Or
\\ Output: 6
$show= $data[2][1];

?>
</body>
</html>

【讨论】:

  • 这很奇怪,因为我在 Safari、Firefox、Chrome 和 IE11 上再次测试了它,使用 GET 或 POST 值我得到相同的结果,一个包含 4 个键而不是 3 个键的数组,即 5 和 6 个值没有被组合在一起:/
  • 是的,我明白了:Array ( [0] =&gt; Array ( [0] =&gt; 2 ) [1] =&gt; Array ( [0] =&gt; 3 ) [2] =&gt; Array ( [0] =&gt; 5 ) [3] =&gt; Array ( [0] =&gt; 6 ) )
  • 我期待的是Array ( [0] =&gt; Array ( [0] =&gt; 2 ) [1] =&gt; Array ( [0] =&gt; 3 ) [2] =&gt; Array ( [0] =&gt; 5 , [1] =&gt; 6 ) )!不过别担心,我的问题已经在上面解决了:)
  • 这正是我用我的代码得到的!数组 ( [0] => 数组 ( [0] => 2 ) [1] => 数组 ( [0] => 3 ) [2] => 数组 ( [0] => 5 [1] => 6 ) ) 我用的是铬
猜你喜欢
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 2013-02-20
  • 2018-08-02
  • 2013-02-21
  • 2014-12-23
相关资源
最近更新 更多