簡體   English   中英

多維數組

[英]multidimensional array

我有一個這樣的多維數組

$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);
                        ?>

這是工作代碼

我怎么能得到這個任何建議...?

您的問題缺少遞歸后關閉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>

將提供的所有代碼與數組放在一頁中。

為了獲得更好的Codding標准,您應該分隔樣式html和php,以試試運氣;)

在調用遞歸時,您沒有在正確的位置關閉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中找到一個有效的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM