手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

旧文:QeePHP中的优秀函数(二)

首页 > PHP >

这两个函数来自于Helper_Array,我觉得是非常常用的方法,功能也比较强大。适合大家使用。

PHP代码
  1. /** 
  2.  * 将一个平面的二维数组按照指定的字段转换为树状结构 
  3.  * 
  4.  * 用法: 
  5.  * @code php 
  6.  * $rows = array( 
  7.  *     array('id' => 1, 'value' => '1-1', 'parent' => 0), 
  8.  *     array('id' => 2, 'value' => '2-1', 'parent' => 0), 
  9.  *     array('id' => 3, 'value' => '3-1', 'parent' => 0), 
  10.  * 
  11.  *     array('id' => 7, 'value' => '2-1-1', 'parent' => 2), 
  12.  *     array('id' => 8, 'value' => '2-1-2', 'parent' => 2), 
  13.  *     array('id' => 9, 'value' => '3-1-1', 'parent' => 3), 
  14.  *     array('id' => 10, 'value' => '3-1-1-1', 'parent' => 9), 
  15.  * ); 
  16.  * 
  17.  * $tree = Helper_Array::tree($rows, 'id', 'parent', 'nodes'); 
  18.  * 
  19.  * dump($tree); 
  20.  *   // 输出结果为: 
  21.  *   // array( 
  22.  *   //   array('id' => 1, ..., 'nodes' => array()), 
  23.  *   //   array('id' => 2, ..., 'nodes' => array( 
  24.  *   //        array(..., 'parent' => 2, 'nodes' => array()), 
  25.  *   //        array(..., 'parent' => 2, 'nodes' => array()), 
  26.  *   //   ), 
  27.  *   //   array('id' => 3, ..., 'nodes' => array( 
  28.  *   //        array('id' => 9, ..., 'parent' => 3, 'nodes' => array( 
  29.  *   //             array(..., , 'parent' => 9, 'nodes' => array(), 
  30.  *   //        ), 
  31.  *   //   ), 
  32.  *   // ) 
  33.  * @endcode 
  34.  * 
  35.  * 如果要获得任意节点为根的子树,可以使用 $refs 参数: 
  36.  * @code php 
  37.  * $refs = null; 
  38.  * $tree = Helper_Array::tree($rows, 'id', 'parent', 'nodes', $refs); 
  39.  *  
  40.  * // 输出 id 为 3 的节点及其所有子节点 
  41.  * $id = 3; 
  42.  * dump($refs[$id]); 
  43.  * @endcode 
  44.  * 
  45.  * @param array $arr 数据源 
  46.  * @param string $key_node_id 节点ID字段名 
  47.  * @param string $key_parent_id 节点父ID字段名 
  48.  * @param string $key_childrens 保存子节点的字段名 
  49.  * @param boolean $refs 是否在返回结果中包含节点引用 
  50.  * 
  51.  * return array 树形结构的数组 
  52.  */  
  53. static function toTree($arr$key_node_id$key_parent_id = 'parent_id',  
  54.                        $key_childrens = 'childrens', & $refs = null)  
  55. {  
  56.     $refs = array();  
  57.     foreach ($arr as $offset => $row)   
  58.     {  
  59.         $arr[$offset][$key_childrens] = array();  
  60.         $refs[$row[$key_node_id]] =& $arr[$offset];  
  61.     }  
  62.   
  63.     $tree = array();  
  64.     foreach ($arr as $offset => $row)   
  65.     {  
  66.         $parent_id = $row[$key_parent_id];  
  67.         if ($parent_id)  
  68.         {  
  69.             if (!isset($refs[$parent_id]))  
  70.             {  
  71.                 $tree[] =& $arr[$offset];  
  72.                 continue;  
  73.             }  
  74.             $parent =& $refs[$parent_id];  
  75.             $parent[$key_childrens][] =& $arr[$offset];  
  76.         }  
  77.         else  
  78.         {  
  79.             $tree[] =& $arr[$offset];  
  80.         }  
  81.     }  
  82.   
  83.     return $tree;  
  84. }  
  85.   
  86. /** 
  87.  * 将树形数组展开为平面的数组 
  88.  * 
  89.  * 这个方法是 tree() 方法的逆向操作。 
  90.  * 
  91.  * @param array $tree 树形数组 
  92.  * @param string $key_childrens 包含子节点的键名 
  93.  * 
  94.  * @return array 展开后的数组 
  95.  */  
  96. static function treeToArray($tree$key_childrens = 'childrens')  
  97. {  
  98.     $ret = array();  
  99.     if (isset($tree[$key_childrens]) && is_array($tree[$key_childrens]))  
  100.     {  
  101.         $childrens = $tree[$key_childrens];  
  102.         unset($tree[$key_childrens]);  
  103.         $ret[] = $tree;  
  104.         foreach ($childrens as $node)   
  105.         {  
  106.             $ret = array_merge($ret, self::treeToArray($node$key_childrens));  
  107.         }  
  108.     }  
  109.     else  
  110.     {  
  111.         unset($tree[$key_childrens]);  
  112.         $ret[] = $tree;  
  113.     }  
  114.     return $ret;  
  115. }  

不过显而易见,这两个函数,都不需要多介绍,tree2list,list2tree,想想也知道怎么用,再加上注释又比较全。
可惜QeePHP不再开发,而ThinkPHP积下来的问题又很多,改动起来也非常痛苦。所以我开始慢慢分析一下。



本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: qeephp

« 上一篇 | 下一篇 »

只显示10条记录相关文章

QEE PHP 发布 (浏览: 33534, 评论: 5)
Drupal 的钩子(Hooks) (浏览: 24416, 评论: 1)
旧文:QeePHP中的优秀函数(一) (浏览: 14055, 评论: 0)
旧文:QeePHP中的优秀函数(三) (浏览: 13555, 评论: 0)

发表评论

评论内容 (必填):