Home > Article > Backend Development > Code example sharing for generating unlimited column trees in PHP
This article mainly introduces the relevant knowledge of generating unlimited column trees in PHP. Has very good reference value. Let’s take a look with the editor below
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); ?>We have two methods to design software: one is to let It is simple enough that bugs cannot hide; the other is to make it complex enough that bugs cannot be found. The former is more difficult.
The above is the detailed content of Code example sharing for generating unlimited column trees in PHP. For more information, please follow other related articles on the PHP Chinese website!