如果網站採用了無限欄位結構,我們可以將網站所有欄位取得出來組成一個樹狀結構。本文主要介紹了php生成無限欄目樹的相關知識。具有很好的參考價值。下
欄位陣列:
$arr=Array( Array('cid' => 2,'cname' => '新闻','pid' => 0), Array('cid' => 4,'cname' =>'体育','pid' => 0), Array('cid' => 5,'cname' => '娱乐','pid' => 0), Array('cid' => 7,'cname' => '热点新闻','pid' =>2), Array('cid' => 8,'cname' => '小众新闻','pid' => 2), Array('cid' => 9,'cname' => '民谣新闻','pid' => 8), );
'pid','id'=>'id','children'=>'children')){ $arr = array(); foreach ($array as &$v) { if ($v[$field['pid']] == $pid) { $v['level'] = $level; $tem = formatTree($array, $v[$field['id']],$v['level']+1,$field); //判断是否存在子数组 $tem && $v[$field['children']] = $tem; $arr[] = $v; } } return $arr; } $tree = formatTree( $arr,0,1, $field = array('pid'=>'pid','id'=>'cid','children'=>'children') ); echo ''; print_r( $tree); ?>以上是php產生無限欄目樹範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!