dongliao1860 2014-03-22 08:40
浏览 13

多维数组到html MultiDimensional复选框

I've an array like this:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => a
                            [1] => d
                            [2] => Array(...)
                        )

                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => a
                            [1] => e
                            [2] => Array(...)
                        )

                )

        )

)

I would like to create, from this array, an set of checkbox nested in HTML <li> statement. I've created a function for make the nested <li>

function nested_li(array $array){
    $output = '<ul>';
    foreach($array as $key => $value){
        $output .= "<li><strong>$key: </strong>
";
        if(is_array($value)){
            $output .= nested_li($value)."
";
        }else{
            $output .= $value."
";
        }
        $output .= '</li>'."
";
    }
    $output .= '</ul>'."
";
    return $output;
}

Now the trouble is to create in this a nested checkbox list, for example:

<input type="checkbox" name="check[0][0][0][0]" value="a"/>
<input type="checkbox" name="check[0][0][0][1]" value="d"/>
<input type="checkbox" name="check[1][0][0][0]" value="a"/>
<input type="checkbox" name="check[1][0][0][1]" value="e"/>

I need to do this for a category system, for choose what category to display.

  • 写回答

1条回答 默认 最新

  • dourou9477 2014-03-22 10:05
    关注

    Hope this helps you :)

    use recursiveiteratoriterator

    $arr=array
    (
        0 => array
            (
                0 => array
                    (
                        0 => array
                            (
                                0 => 'a',
                                1 => 'd',
                                2 => 's' //or Array(...)
                            ),
    
                    ),
    
            ),
    
        1 => array
            (
                0 => array
                    (
                        0 => array
                            (
                                0 => 'a',
                                1 => 'e',
                                2 => 'l'
                            ),
    
                    ),
    
            ),
    
    );
    
    $flat = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
    foreach($flat as $item) {
      $a[]= $item;
    }
    foreach($a as $i)
    {
        echo '<input type="checkbox" name="'.$i.'" value="a"/>'.$i;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)