array('id'=>'1','pid'=>0,'name'=>'一级栏目一'), * 2 => array('id'=>'2','pid'=>0,'name'=>'一级栏目二'), * 3 => array('id'=>'3','pid'=>1,'name'=>'二级栏目一'), * 4 => array('id'=>'4','pid'=>1,'name'=>'二级栏目二'), * 5 => array('id'=>'5','pid'=>2,'name'=>'二级栏目三'), * 6 => array('id'=>'6','pid'=>3,'name'=>'三级栏目一'), * 7 => array('id'=>'7','pid'=>3,'name'=>'三级栏目二') * ) */ public function init($arr = array()) { $this->arr = $this->ret = NULL; $this->arr = $arr; return is_array($arr); } /** * 得到父级数组 * * @param int * @return array */ public function get_parent($myid) { $newarr = array(); if(!isset($this->arr[$myid])) return FALSE; $pid = $this->arr[$myid]['pid']; $pid = $this->arr[$pid]['pid']; if (is_array($this->arr)) { foreach ($this->arr as $id => $a) { if ($a['pid'] == $pid) $newarr[$id] = $a; } } return $newarr; } /** * 得到子级数组 * * @param int * @return array */ public function get_child($myid) { $a = $newarr = array(); if (is_array($this->arr)) { foreach ($this->arr as $id => $a) { if ($a['pid'] == $myid) $newarr[$id] = $a; } } return $newarr ? $newarr : FALSE; } /** * 获取子栏目json * * Enter description here ... * @param unknown_type $myid */ public function creat_sub_json($myid, $str='') { $sub_cats = $this->get_child($myid); $n = 0; if(is_array($sub_cats)) foreach($sub_cats as $c) { $data[$n]['id'] = iconv(CHARSET,'utf-8',$c['catid']); if($this->get_child($c['catid'])) { $data[$n]['liclass'] = 'hasChildren'; $data[$n]['children'] = array(array('text'=>' ','classes'=>'placeholder')); $data[$n]['classes'] = 'folder'; $data[$n]['text'] = iconv(CHARSET,'utf-8',$c['catname']); } else { if($str) { @extract(array_iconv($c,CHARSET,'utf-8')); eval("\$data[$n]['text'] = \"$str\";"); } else { $data[$n]['text'] = iconv(CHARSET,'utf-8',$c['catname']); } } $n++; } return json_encode($data); } private function have($list,$item){ return(strpos(',,'.$list.',',','.$item.',')); } }