0

我有一个像这样的多维数组

$role = array (
                  'Dashboard' => null,
                  'Students' => 
                    array (
                      'Admission' => array ( 0 =>  'Entry' ,1 =>  'Review' , 2 =>  'Approved/Reject'
                       ),
                     'Search' => null,
                     'AdvanceSearch' => null,
                     'Courses' => null
                    ),
                  'HR' => array ('Settings' => null),
                  'Employee Management' => array ( 0 =>  'Entry',1 =>  'Association',2 =>  'Search' ),
                  'Employee Attendance' => null,
                  'Payroll' => array (0 =>  'Generate Pay Slip',1 =>  'Payroll Run' , 2 =>  'Payroll Revert',3 =>  'View Pay Slips'),
                  'Finance' => null,
                  'More' => null);

我想在我的html中打印这个数组结果

这是想要的解决方案

我试图通过使用递归来做到这一点,但由于 DIV 没有正确关闭而无法做到这一点.. 这是我在我的 html 中尝试的代码

<?php 

                             $child = 0;
                            function RecursiveWrite($array, $child ) {
                                $parent_array_size =  count($array);
                                foreach ($array as $key => $vals) {
                                    if(is_array($vals)){
                                        $inner_array_size = count($vals);
                                        echo "<div class='main clear'><input type='checkbox'/> ".$key." &nbsp; &nbsp; ";
                                        RecursiveWrite($vals,$child);
                                    }else{
                                        $child = 0;
                                        if(is_numeric($key)){
                                            echo " &nbsp; <div class='left'> &nbsp; &nbsp; <input type='checkbox' class=''/> ".$vals." &nbsp; &nbsp; </div></div>";
                                        }else{
                                            echo "<div class='clear'><input type='checkbox'/> ".$key." </div></div>";
                                        }
                                    }
                                    //
                                }
                            }
                            RecursiveWrite($role, $child);
                        ?>

这是工作代码

我怎样才能得到这个任何建议......?

4

2 回答 2

3

您的问题是递归后缺少关闭 div 试试这个函数

function RecursiveWrite($array, $child )
{
    $parent_array_size =  count($array);
    foreach ($array as $key => $vals)
     {
        if(is_array($vals))
        {
            $inner_array_size = count($vals);
            echo "<div class='deep'><div class='extra'><input type='checkbox'/> ".$key."</div>";
            RecursiveWrite($vals,$child);
            echo "</div>";
        }
        else
        {
            if(is_numeric($key)){
                echo "<span class='single'><input type='checkbox' class=''/> ".$vals."</span>";
            }else{
                echo "<div class='single'><input type='checkbox'/> ".$key." </div>";
            }

        }
    }
}

使用这个 CSS

<style type="text/css">
    .parent {border: solid 1px #000;}
    .parent div {border: solid 1px #000; margin:5px;}
    .extra {border:0px !important;}
    .single {margin-left:10px !important; border:0px !important;}
    span.single {display:inline-block; margin-left:20px;}
</style>

用它来调用你的函数

<div class="parent"><? RecursiveWrite($role, $child);?></div>

将提供的所有代码与您的数组放在一页中。

为了获得更好的编码标准,您应该将您的样式 html 和 php 分开来试试运气;)

于 2012-08-29T07:00:12.113 回答
1

在调用递归时,您没有div在正确的位置关闭,在结束递归之后您需要编写结尾div

  • 打开 div
  • 运行递归
  • 关闭 div

同样,您有两个不必要的 div 关闭。始终确保打开与关闭一样多的 div,反之亦然。我已经在代码中标记了需要更改的地方。

代码:

<?php 
    $child = 0;
    function RecursiveWrite($array, $child ) {
        $parent_array_size =  count($array);
        foreach ($array as $key => $vals) {
            if(is_array($vals)){
                $inner_array_size = count($vals);
                echo "<div class='main clear'><input type='checkbox'/> ".$key." &nbsp; &nbsp; ";
                RecursiveWrite($vals,$child);
                echo "</div>"; /* <== ADD THIS */
            }else{
                $child = 0;
                if(is_numeric($key)){
                    echo " &nbsp; <div class='left'> &nbsp; &nbsp; <input type='checkbox' class=''/> ".$vals." &nbsp; &nbsp; </div>"; /* <== REMOVE EXTRA DIV */
                }else{
                    echo "<div class='clear'><input type='checkbox'/> ".$key." </div>";  /* <== REMOVE EXTRA DIV */
                }
            }
            //
        }
    }
    RecursiveWrite($role, $child);
?>

您可以在http://codepad.viper-7.com/507rLc找到一个工作示例

于 2012-08-29T06:53:07.310 回答