Home  >  Article  >  Backend Development  >  PHP generates unlimited column tree sample code

PHP generates unlimited column tree sample code

怪我咯
怪我咯Original
2017-07-12 11:14:321374browse

If the website adopts an unlimited column structure, we can obtain all the columns of the website to form a tree structure. This article mainly introduces the relevant knowledge about generating unlimited column trees in PHP. Has very good reference value. Next

ColumnArray:

$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); 
?> 

The above is the detailed content of PHP generates unlimited column tree sample code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn