Last active
July 15, 2022 11:15
-
-
Save forecho/f8b2e657cbabaef7deaa to your computer and use it in GitHub Desktop.
Revisions
-
forecho revised this gist
Jun 13, 2016 . 2 changed files with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,7 @@ <?php /** * @link http://ideone.com/gAsKlI */ $items = array( array('id' => 1, 'pid' => 0, 'name' => '一级11'), array('id' => 11, 'pid' => 0, 'name' => 'www.111cn.net 一级12'), This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ <?php /** * 递归无限级分类【先序遍历算】,获取任意节点下所有子孩子 * @link http://ideone.com/pN8FPt * @param array $arrCate 待排序的数组 * @param int $parent_id 父级节点 * @param int $level 层级数 -
forecho revised this gist
Jun 13, 2016 . No changes.There are no files selected for viewing
-
forecho revised this gist
Jun 13, 2016 . 1 changed file with 47 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ <?php /** * 递归无限级分类【先序遍历算】,获取任意节点下所有子孩子 * @param array $arrCate 待排序的数组 * @param int $parent_id 父级节点 * @param int $level 层级数 * @return array $arrTree 排序后的数组 */ function getMenuTree($arrCat, $parent_id = 0, $level = 0) { static $arrTree = array(); //使用static代替global if( empty($arrCat)) return FALSE; $level++; foreach($arrCat as $key => $value) { if($value['parent_id' ] == $parent_id) { $value[ 'level'] = $level; $arrTree[] = $value; unset($arrCat[$key]); //注销当前节点数据,减少已无用的遍历 getMenuTree($arrCat, $value[ 'id'], $level); } } return $arrTree; } /** * 测试数据 */ $arrCate = array( //待排序数组 array( 'id'=>1, 'name' =>'顶级栏目一', 'parent_id'=>0), array( 'id'=>2, 'name' =>'顶级栏目二', 'parent_id'=>0), array( 'id'=>3, 'name' =>'栏目三', 'parent_id'=>1), array( 'id'=>4, 'name' =>'栏目四', 'parent_id'=>3), array( 'id'=>5, 'name' =>'栏目五', 'parent_id'=>4), array( 'id'=>6, 'name' =>'栏目六', 'parent_id'=>2), array( 'id'=>7, 'name' =>'栏目七', 'parent_id'=>6), array( 'id'=>8, 'name' =>'栏目八', 'parent_id'=>6), array( 'id'=>9, 'name' =>'栏目九', 'parent_id'=>7), ); header('Content-type:text/html; charset=utf-8'); //设置utf-8编码 echo '<pre>'; print_r(getMenuTree($arrCate, 0, 0)); echo '</pre>'; -
forecho revised this gist
Jan 27, 2016 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ <?php $items = array( array('id' => 1, 'pid' => 0, 'name' => '一级11'), array('id' => 11, 'pid' => 0, 'name' => 'www.111cn.net 一级12'), -
forecho revised this gist
Jan 27, 2016 . 1 changed file with 25 additions and 25 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,29 +30,29 @@ function formatTree($array, $pid = 0) Array ( [0] => Array ( [id] => 1 [pid] => 0 [name] => 一级11 [children] => Array ( [0] => Array ( [id] => 2 [pid] => 1 [name] => 二级21 ) [1] => Array ( [id] => 3 [pid] => 1 [name] => 二级23 ) [2] => Array ( [id] => 9 [pid] => 1 [name] => 二级25 ) @@ -62,29 +62,29 @@ function formatTree($array, $pid = 0) ) [1] => Array ( [id] => 11 [pid] => 0 [name] => www.111cn.net 一级12 [children] => Array ( [0] => Array ( [id] => 10 [pid] => 11 [name] => 二级22 ) [1] => Array ( [id] => 12 [pid] => 11 [name] => 二级24 [children] => Array ( [0] => Array ( [id] => 13 [pid] => 12 [name] => 三级31 ) -
forecho revised this gist
Jan 27, 2016 . 1 changed file with 3 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,7 @@ function formatTree($array, $pid = 0) [id] => 1 [pid] => 0 [name] => 一级11 [children] => Array ( [0] => Array ( @@ -66,7 +66,7 @@ function formatTree($array, $pid = 0) [id] => 11 [pid] => 0 [name] => www.111cn.net 一级12 [children] => Array ( [0] => Array ( @@ -80,7 +80,7 @@ function formatTree($array, $pid = 0) [id] => 12 [pid] => 11 [name] => 二级24 [children] => Array ( [0] => Array ( @@ -98,5 +98,4 @@ function formatTree($array, $pid = 0) ) ) */ -
forecho revised this gist
Jan 27, 2016 . 1 changed file with 76 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,4 +24,79 @@ function formatTree($array, $pid = 0) } echo "<pre>"; print_r(formatTree($items)); /** Array ( [0] => Array ( [id] => 1 [pid] => 0 [name] => 一级11 [son] => Array ( [0] => Array ( [id] => 2 [pid] => 1 [name] => 二级21 ) [1] => Array ( [id] => 3 [pid] => 1 [name] => 二级23 ) [2] => Array ( [id] => 9 [pid] => 1 [name] => 二级25 ) ) ) [1] => Array ( [id] => 11 [pid] => 0 [name] => www.111cn.net 一级12 [son] => Array ( [0] => Array ( [id] => 10 [pid] => 11 [name] => 二级22 ) [1] => Array ( [id] => 12 [pid] => 11 [name] => 二级24 [son] => Array ( [0] => Array ( [id] => 13 [pid] => 12 [name] => 三级31 ) ) ) ) ) ) */ -
forecho revised this gist
Jan 27, 2016 . 1 changed file with 21 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,26 +1,26 @@ $items = array( array('id' => 1, 'pid' => 0, 'name' => '一级11'), array('id' => 11, 'pid' => 0, 'name' => 'www.111cn.net 一级12'), array('id' => 2, 'pid' => 1, 'name' => '二级21'), array('id' => 10, 'pid' => 11, 'name' => '二级22'), array('id' => 3, 'pid' => 1, 'name' => '二级23'), array('id' => 12, 'pid' => 11, 'name' => '二级24'), array('id' => 13, 'pid' => 12, 'name' => '三级31'), array('id' => 9, 'pid' => 1, 'name' => '二级25'), ); function formatTree($array, $pid = 0) { $arr = $tem = array(); foreach ($array as $v) { if ($v['pid'] == $pid) { $tem = formatTree($array, $v['id']); //判断是否存在子数组 $tem && $v['children'] = $tem; $arr[] = $v; } } return $arr; } echo "<pre>"; -
forecho created this gist
Jan 27, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ $items = array( array('id' => 1, 'pid' => 0, 'name' => '一级11' ), array('id' => 11, 'pid' => 0, 'name' => 'www.111cn.net 一级12' ), array('id' => 2, 'pid' => 1, 'name' => '二级21' ), array('id' => 10, 'pid' => 11, 'name' => '二级22' ), array('id' => 3, 'pid' => 1, 'name' => '二级23' ), array('id' => 12, 'pid' => 11, 'name' => '二级24' ), array('id' => 13, 'pid' => 12, 'name' => '三级31' ), array('id' => 9, 'pid' => 1, 'name' => '二级25' ), ); function formatTree($array, $pid = 0){ $arr = array(); $tem = array(); foreach ($array as $v) { if ($v['pid'] == $pid) { $tem = formatTree($array, $v['id']); //判断是否存在子数组 $tem && $v['children'] = $tem; $arr[] = $v; } } return $arr; } echo "<pre>"; print_r(formatTree($items));