本文实例讲述了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程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 金池VS刘罡2009《双声情》[WAV+CUE]
- 冯奥迪《卄》[320K/MP3][67.08MB]
- 冯奥迪《卄》[FLAC/分轨][172.46MB]
- Merryland《Merryland》[320K/MP3][69.48MB]
- 苏芮.1991-停在我心里的温柔【福茂】【WAV+CUE】
- 苏慧伦.2020-面面Every.Side.of.Me【相信音乐】【WAV+CUE】
- 群星.1991-不一样的感觉REMIX【宝丽金】【WAV+CUE】
- 银霞.1983-《你那好冷的小手》珍藏纪念版[WAV分轨]
- 银霞.2002-《国语巨星专辑VOL.3》2CD台湾版[WAV+CUE]
- 金池刘罡《昨日情歌HQ》头版限量编号[低速原抓WAV+CUE]
- 段奥娟.2024-23·岁碎念【白米范】【FLAC分轨】
- 顺子.2001-昨日·唯一·更多·顺子自选辑【魔岩】【WAV+CUE】
- 无印良品.1999-私藏三首·原创相对论(EP)【滚石】【WAV+CUE】
- Merryland《Merryland》[FLAC/分轨][170.22MB]
- 庄锭欣 ELKIE《没有你的未来》[320K/MP3][15.78MB]