层次结构数组<table> </table>的php树标记输出

时间:2014-09-28 06:49:27

标签: php arrays treeview parent-child hierarchy

我们有来自mysqli查询输出的这个数组:

Array
(
    [0] => Array
        (
            [value] => value1
            [id] => 1
            [title] => title1
            [parent_id] => 0
        )

    [1] => Array
        (
            [value] => value2
            [id] => 2
            [title] => title2
            [parent_id] => 1
        )

    [2] => Array
        (
            [value] => value3
            [id] => 3
            [title] => title3
            [parent_id] => 0
        )

    [3] => Array
        (
            [value] => value4
            [id] => 4
            [title] => title4
            [parent_id] => 3
        )

)

然后我们需要使用php函数渲染这个html输出:

<table  id="1" >
     <!-- thead for  parent_id==0 index -->
     <thead>
           <tr>
               <td width="40%">title1</td>
               <td width="60%">value1</td>
           </tr>
    </thead> 

   <!-- childrens -->
    <tr>
        <td>title2</td>
        <td>value2</td>
    </tr>
</table>


<table  id="2" >
     <!-- thead for  parent_id==0 index -->
     <thead>
           <tr>
               <td width="40%">title3</td>
               <td width="60%">value3</td>
           </tr>
    </thead> 

   <!-- childrens -->
    <tr>
        <td>title4</td>
        <td>value4</td>
    </tr>
</table>
谁能帮助我吗?可能这很容易,但我已经尝试过一切都没有成功!!

这样的事情:

<?php

    function generateTree($datas, $parent = 0, $limit=0){
    if($limit > 1000) return ''; // Make sure not to have an endless recursion
    $tree = '';
    $tree = '<table>';
    for($i=0, $ni=count($datas); $i < $ni; $i++){
            if($datas[$i]['parent_id'] == $parent){
                $tree .= '<tr>';
                $tree .= $datas[$i]['title'].'='.$datas[$i]['value'];
                $tree .= generateTree($datas, $datas[$i]['id'], $limit++);
                $tree .= '</tr>';
            }
    }
    $tree .= '</table>';
    return $tree;
    }

  echo generateTree($myArray);
?>

1 个答案:

答案 0 :(得分:0)

这就是答案

//index elements by id
foreach ($resutls as $item) {
        $item['subs'] = array();
        $indexedItems[$item['id']] = (object) $item;
}

//assign to parent
$topLevel = array();
foreach ($indexedItems as $item) {
        if ($item->parent_id == 0) {
            $topLevel[] = $item;
        } else {
            $indexedItems[$item->parent_id]->subs[] = $item;
        }
}


//recursive function
function renderTree($items) {        
$render = "";
$i=0;
foreach ($items as $item) 
  {
        if(!empty($item->subs))
            {
                $i++;
                 $render .= '<table  id="t'.$i.'">'."\n<thead>\n<tr>\n<td width='40%'>" . $item->title."</td>\n<td width='60%'></td>\n</tr>\n</thead>\n";
            }else{
                  $render .= "\n<tr>\n<td>" . $item->title."</td>\n<td>".$item->value."</td>\n</tr>\n";
            }

            if (!empty($item->subs))
            {
                $render .= renderTree($item->subs);
            }
            if(!empty($item->subs))
            {
              $render .= "</table>\n\n";
            }
     }

         return $render;
  }

  echo renderTree($topLevel);