簡體   English   中英

將父/子 php 數組轉換為 html 表

[英]Convert parent/child php array to html table

我有以下 PHP 陣列。

Array
(
 [168] => Array
    (
        [link] => asdfasdf
        [children] => Array
            (
                [239] => Array
                    (
                        [link] => tascatestlalal
                        [children] => Array
                            (
                            )
                    )
                [240] => Array
                    (
                        [link] => otrotestttt
                        [children] => Array
                            (
                            )
                    )
            )
    )
[229] => Array
    (
        [link] => Sub-task ex
        [children] => Array
            (
            )
    )

[230] => Array
    (
        [link] => Sub-task test
        [children] => Array
            (
                [231] => Array
                    (
                        [link] => tasktest1
                        [children] => Array
                            (
                            )
                    )
                [232] => Array
                    (
                        [link] => tasktest 2
                        [children] => Array
                            (
                                [233] => Array
                                    (
                                        [link] => tasktest 5
                                        [children] => Array
                                            (
                                                [235] => Array
                                                    (
                                                        [link] => tasca235
                                                        [children] => Array
                                                            (
                                                            )
                                                    )
                                            )
                                    )
                            )
                    )
                [234] => Array
                    (
                        [link] => tasca234
                        [children] => Array
                            (
                            )
                    )
            )
    )
 )  

我需要把它轉換成這個

<table>
 <tr>
 <td>
 Sub-task
 </td>
 </tr>
 <tr>
 <td>
 --Tasktest1
  </td>
  </tr>
  <tr>
  <td>
  --tasktest 2
  </td>
  </tr>
  <tr>
  <td>
  ---tasktest 5
  </td>
  </tr>
  <tr>
  <td>
  ----tasca235
  </td>
  </tr>
  <tr>
  <td>
  --tasca234
  </td>
  </tr>
 </table>

這個我已經知道如何用這個 function 的列表來做到這一點,但看不到修改這個 function 以將其轉換為上面的表格示例:(。一些幫助將不勝感激

function ArrayToHTMLList($arr) {
$str = "<ul class='tasklist'>";
foreach($arr as $key => $value) {

        if((!empty($value))){

          $str .="<li>";

            if(is_array($value))
                  $str .= ArrayToHTMLList($value);
            else                  
                $str .= $value;

          $str .= "</li>";         
        }

}

$str .= "</ul>";
return $str;
}
function nestedArrayToFlatList($node, $indent) {
  $str = '';

  if (isset($node['link'])) {
    $str .= '<tr><td>' . $indent . $node['link'] . '</td></tr>';
  }

  if (isset($node['children']) && count($node['children']) > 0) {
    foreach ($node['children'] as $child) {
      $str .= nestedArrayToFlatList($child, '--' . $indent);
    }
  }

  return $str;
}


$myList = '<table>' . nestedArrayToFlatList($myMassiveArray, '') . '</table>';

暫無
暫無

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

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