Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1183631
  • 博文数量: 252
  • 博客积分: 5421
  • 博客等级: 大校
  • 技术积分: 2418
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-17 12:59
文章分类

全部博文(252)

文章存档

2017年(3)

2016年(18)

2015年(31)

2014年(18)

2013年(7)

2012年(8)

2011年(12)

2010年(30)

2009年(32)

2008年(57)

2007年(36)

分类: PHP

2014-01-15 02:17:46


  1. function fileTreeToPathList($arrFileTree, $strPrevKey='')
  2. {
  3.     $arrPathList = array();
  4.     foreach ($arrFileTree as $k => $node) {
  5.         $strNowKey = $strPrevKey ? $strPrevKey.'/'.$k : $k;
  6.         if (is_array($node)) {
  7.             $arr = fileTreeToPathList($node, $strNowKey);
  8.             $arrPathList = array_merge($arrPathList, $arr);
  9.         } else {
  10.             $arrPathList[$strNowKey] = $node;
  11.         }
  12.     }
  13.     return $arrPathList;
  14. }

  15. class nodeclass{}

  16. $arrFileTree = array(
  17.     'a' => new nodeclass,
  18.     'b' => new nodeclass,
  19.     'c' => array(
  20.         'd' => new nodeclass,
  21.         'e' => new nodeclass,
  22.         'f' => array(
  23.             'g' => new nodeclass,
  24.             'h' => new nodeclass,
  25.         ),
  26.     )
  27. );

  28. $arrPathList = fileTreeToPathList($arrFileTree);
  29. print_r($arrPathList);

  30. // -- output --
  31. Array
  32. (
  33.     [a] => nodeclass Object
  34.         (
  35.         )

  36.     [b] => nodeclass Object
  37.         (
  38.         )

  39.     [c/d] => nodeclass Object
  40.         (
  41.         )

  42.     [c/e] => nodeclass Object
  43.         (
  44.         )

  45.     [c/f/g] => nodeclass Object
  46.         (
  47.         )

  48.     [c/f/h] => nodeclass Object
  49.         (
  50.         )
  51. )


阅读(1581) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~