arr = $arr; $this->icon = array( '│', '├', '└' ); $this->ret = ''; //return is_array($arr); } public function getParant( $myid ) { $newarr = array(); if (!isset($this->arr[$myid])) { return false; } $pid = $this->arr[$myid]['parent_id']; $pid = $this->arr[$pid]['parent_id']; if (is_array($this->arr)) { foreach ($this->arr as $id => $a) { if ($a['parent_id'] == $pid) { $newarr[$id] = $a; } } } return $newarr; } public function getChild( $myid ) { $newarr = array(); if (is_array($this->arr)) { foreach ($this->arr as $id => $a) { if ($a['parent_id'] == $myid) { $newarr[$id] = $a; } } } return $newarr ? $newarr : false; } public function getPos( $myid, &$newarr ) { $a = array(); if (!isset($this->arr[$myid])) { return false; } $newarr[] = $this->arr[$myid]; $pid = $this->arr[$myid]['parent_id']; if (isset($this->arr[$pid])) { $this->getPos( $pid, $newarr ); } if (is_array($newarr)) { krsort($newarr); foreach ($newarr as $v) { $a[$v['id']] = $v; } } return $a; } public function getTree( $myid, $str, $sid = 0, $adds = '' ) { $nstr = ''; $number = 1; $child = $this->getChild($myid); if (is_array($child)) { $total = count($child); foreach ($child as $id => $a) { $j = $k = ''; if ($number == $total) { $j .= $this->icon[2]; } else { $j .= $this->icon[1]; $k = $adds ? $this->icon[0] : ''; } $spacer = $adds ? $adds.$j : ''; $selected = $id == $sid ? 'selected' : ''; @extract($a); eval("\$nstr = \"$str\";"); $this->ret .= $nstr; $this->getTree( $id, $str, $sid, $adds . $k . ' ' ); $number++; } } return $this->ret; } /** * +---------------------------------------------------------- * 返回属性节点数组 * +---------------------------------------------------------- * @access public * @param array $arr 原始数组 * @param string $key_node_id 子级ID名 * @param string $key_parent_id 父级ID名 * @param string $key_childrens 节点名 * @param string $refs 资源 * +---------------------------------------------------------- * @return array * +---------------------------------------------------------- */ public function tree2AreaArray( $arr, $key_node_id, $key_parent_id = 'parent_id', $key_childrens = 'childrens', & $refs = null ) { if (empty($arr)) { $arr = $this->arr; } $refs = array(); foreach ($arr as $offset => $row) { $arr[$offset][$key_childrens] = array(); $refs[$row[$key_node_id]] =& $arr[$offset]; } $tree = array(); foreach ($arr as $offset => $row) { $parent_id = $row[$key_parent_id]; if ($parent_id) { if (!isset($refs[$parent_id])) { $tree[] =& $arr[$offset]; continue; } $parent =& $refs[$parent_id]; $parent[$key_childrens][$arr[$offset][$key_node_id]] = &$arr[$offset]; if (empty($parent[$key_childrens][$arr[$offset][$key_node_id]][$key_childrens])) { unset($parent[$key_childrens][$arr[$offset][$key_node_id]][$key_childrens]); } } else { $tree[$arr[$offset][$key_node_id]] =& $arr[$offset]; } } return $tree; } }