无为清净楼资源网 Design By www.qnjia.com

本文实例讲述了php实现的树形结构数据存取类。分享给大家供大家参考。

具体实现代码如下:

复制代码 代码如下:<"path数据错误".var_export($item, true));
            }
        }
        
        //print_r(self::$_indexs);
    }
    
    /**
     * 添加子节点
     * 
     * @param array $item
     * @return void
     */
    public function addChild(array $item, $parent = NULL)
    {
        $child          = new Tree();
        $child->_info   = $item;
        $child->_parent = $parent == NULL "id字段不存在或者不为字符串");
        }
    }
    
    /**
     * 获取对自己的引用
     * 
     * @return Tree object quote
     */
    private function _getSelf()
    {
        return $this;
    }
    
    /**
     * 获取指定id的节点的子节点
     * 
     * @param int $id
     * @return Tree object
     */
    public function getChild($id)
    {
        $data       = self::$_indexs[$id]->_child;
        $this->_data = $data;
        return $this;
    }
    
    /**
     * 获取指定id的节点的父节点
     * 
     * @param int $id
     * @return Tree object
     */
    public function getParent($id)
    {
        $data = self::$_indexs[$id]->_parent;
        $this->_data = $data;
        return $this;
    }
    
    /**
     * 获取指定id的节点的同级节点
     *
     * @param int $id
     * @return Tree object
     */
    public function getBrother($id)
    {
        $data = self::$_indexs[$id]->_parent->_child;
        $this->_data = $data;
        return $this;
    }
    
    /**
     * 将Tree对象转化为数组
     * 
     * @param  object $object
     * @return array
     */
     public function toArray($obj = NULL)
     {
        $obj  = ($obj === NULL) "_arr不是数组");
        }
     
        return $arr;
    }
    
    /**
     * 过滤_parent等字段,以免造成无限循环
     * 
     * @param object $obj
     * @return void
     */
    private function _getBaseInfo($obj)
    {
        $vars = get_object_vars($obj);
        $baseInfo['_info']  =  $vars['_info'];
        $baseInfo['_child'] =  $vars['_child'];
        return $baseInfo;
    }
    
    /**
     * 二维数组排序
     *
     * 根据指定的键名对二维数组进行升序或者降序排列
     *
     * @param array  $arr 二维数组
     * @param string $keys
     * @param string $type 必须为 asc或desc
     * @throws 当参数非法时抛出异常
     * @return 返回排序好的数组
     */
    private function _array_sort(array $arr, $keys, $type = 'asc') {
        if (!is_string($keys)) {
            throw new Exception("非法参数keys:参数keys的类型必须为字符串");
        }
    
        $keysvalue = $new_array = array();
        foreach ($arr as $k=>$v) {
            if (!is_array($v) || !isset($v[$keys])) {
                throw new Exception("参数arr不是二维数组或arr子元素中不存在键'{$keys}'");
            }
            $keysvalue[$k] = $v[$keys];
        }
    
        switch ($type) {
            case 'asc':
                asort($keysvalue);
                break;
            case 'desc':
                arsort($keysvalue);
                break;
            default:
                throw new Exception("非法参数type :参数type的值必须为 'asc' 或 'desc'");
        }
    
        reset($keysvalue);
        foreach ($keysvalue as $k=>$v) {
            $new_array[$k] = $arr[$k];
        }
        return $new_array;
    }
}
?>

希望本文所述对大家的PHP程序设计有所帮助。

标签:
php,树形,结构,数据,存取,类

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com