2012-08-29 278 views
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打印這個數組結果作爲

Here is the wanted solution

我試圖這樣做,但無法這樣做,因爲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); 
         ?> 

這裏是working code

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

+1

輸入數據結構是有點不一致。 – pQd

回答

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這個試試你的運氣;)

+0

你提供了我所需要的Thnks @champ – chhameed

+0

你最歡迎:) – Champ

1

你不關閉在適當的地方div,調用遞歸時,結束遞歸之後,你需要寫結束div

  • 開放格
  • 運行遞歸
  • close 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

+0

讓我先檢查... – chhameed

+0

@Hameed退房http://codepad.viper-7.com/507rLc – Kuf

+0

哦謝謝@kuf。你救我一天..乾杯:) – chhameed