从文本构建树

时间:2013-01-29 03:25:55

标签: php multidimensional-array treeview

我有关于PHP的数据,我将从这些数据中创建树状菜单:

$data[] = "IT/managerial/software/kasubsie/Moro/Konsolidasi Server";
$data[] = "IT/managerial/software/kasubsie/Moro/Setting Server";
$data[] = "IT/managerial/software/kasubsie/Setting Server/Moro";

如何使用列生成表格:

| id | parent_id | name | 

1 个答案:

答案 0 :(得分:1)

这很有效。以此为基础。主要逻辑是preg_split每行数据。希望这有帮助!

<?php

$data = array();
$data[] = "IT/managerial/software/kasubsie/Moro/Konsolidasi Server";
$data[] = "IT/managerial/software/kasubsie/Moro/Setting Server";
$data[] = "IT/managerial/software/kasubsie/Setting Server/Moro";

$value_array = array();

echo '<table border="1">';
foreach ($data as $key => $value) {
    echo '<tr>';
    $value_array = preg_split("/\//", $value);
    foreach ($value_array as $table_cell_value) {
        echo '<td>' . $table_cell_value . '</td>';
    }
    echo '</tr>';
}
echo '</table>';

?>