自定义输入数组并将其输出

时间:2012-12-18 17:34:58

标签: php arrays foreach

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

DEMO 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                    
                )

        )

)

1 个答案:

答案 0 :(得分: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][]

Demo