1

我想要获取(处理)值输入和复选框,如下例所示,而不更改复选框中的名称元素,我尝试如下,但这不起作用:

演示http ://codepad.viper-7.com/4ldRKV

<?php
if($_POST){
    //foreach ($_POST['cDA'] as $idx => $value) {
        foreach ($_POST as $idx => $value) {
            echo '<pre>';
            print_r($_POST);
        }
    //}
}
?>
<form action="#" method="post">
    <input type="text" name="cDI[]" value="F1">
        <input type="checkbox" name="cDA[0][]" value="11" checked>
        <input type="checkbox" name="cDA[0][]" value="11" checked>
        <input type="checkbox" name="cDC[1][]" value="22" checked>
        <input type="checkbox" name="cDC[1][]" value="22" checked>
    </br>
    <input type="text" name="cDI[]" value="F2">
        <input type="checkbox" name="cDA[0][]" value="33" checked>
        <input type="checkbox" name="cDA[0][]" value="33" checked>
        <input type="checkbox" name="cDC[][]" value="44" checked>
        <input type="checkbox" name="cDC[][]" value="44" checked>
    </br>
    <input type="text" name="cDI[]" value="F3">
        <input type="checkbox" name="cDA[0][]" value="55" checked>
        <input type="checkbox" name="cDA[0][]" value="55" checked>
        <input type="checkbox" name="cDC[][]" value="66" checked>
        <input type="checkbox" name="cDC[][]" value="66" checked>
    </br>
<input type="submit" value="Submit">
</form>

我希望输出为:(应该如何更改 html 和 php 以获得以下结果?)

Array
(
    [cDI] => Array
        (
            [0] => F1
            [1] => F2
            [2] => F3
        )
    [cDA] => Array
        (
            [0] => Array
                (
                    [0] => 11
                    [1] => 11                    
                )
            [1] => Array
                (
                    [0] => 33
                    [1] => 33                    
                )
            [2] => Array
                (
                    [0] => 55
                    [1] => 55                    
                )

        )
    [cDC] => Array
        (
            [0] => Array
                (
                    [0] => 22
                    [1] => 22                    
                )
            [1] => Array
                (
                    [0] => 44
                    [1] => 44                    
                )
            [2] => Array
                (
                    [0] => 66
                    [1] => 66                    
                )

        )

)
4

1 回答 1

0

如果使用cDA[0][]作为输入框集合的名称,会$_POST['cDA']变成这样:

array(
    0 => array(
        0 => 11,
        1 => 11,
        2 => 33,
        3 => 33,
        4 => 55,
        5 => 55,
    ),
);

使用cDC[][]作为输入框的名称,您最终将得到以下数组$_POST['cDC']

array(
    0 => array(
        0 => 11,
    ),
    0 => array(
        0 => 11,
    ),
    0 => array(
        0 => 33,
    ),
    0 => array(
        0 => 33,
    ),
    0 => array(
        0 => 55,
    ),
    0 => array(
        0 => 55,
    ),
);

要使它们正确分组,您必须明确设置第一个方括号之间的值:

cDA[0][]
cDA[0][]
cDC[0][]
cDC[0][]

cDA[1][]
cDA[1][]
cDC[1][]
cDC[1][]

cDA[2][]
cDA[2][]
cDC[2][]
cDC[2][]

演示

于 2012-12-18T17:43:59.000 回答