<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ``` <?php namespace Org\LAMP; class CatTree { private static $order = 'ord'; //有排序字段和表的對應,如果沒有這個字段可以不寫 private $id = 'typeid'; //表的id字段 private $pid = 'pid'; //表的父級pid字段 private $typename = 'name'; //表字段對應的類型名 private $son = 'subcat'; //如果有子數組,子數組下標,可以自定義值(在新數組中出現) private $level = 'level'; //默認的新加級別下標, 可以自定義值 private $path = 'path'; //默認的路徑下標,可以自定義 private $ps = ','; //默認的路徑分隔符號,可以自己字義 private $childs = 'childs'; //默認的子數組下標,可以自己定義 private $narr = array(); //放分完級別后的數組 private $i; //臨時的一個記數 private $pathname = 'pathname'; //默認的全路徑名,可以自己定義belongs //設置表字段id public function setId($value='') { $this->id=$value; } //表的父級pid字段 public function setPid($value='') { $this->pid=$value; } public function setTypename($value='') { $this->typename=$value; } public function setPathname($value='') { $this->pathname=$value; } /** * * 獲取分類數結構 * 第二個參數為true時,補全父類 如:衣服/上衣/ * @param $items array 從數據庫查詢出來的分類信息(二維數組) * @param $allpath boolean 是否返回全路徑類型名稱 默認不返回提高效率 * @return 返回多維數組 */ public function getTree($items,$allpath=false){ if(empty($items)) return array(); $tree = array(); //格式化的樹 $tmpMap = array(); //臨時扁平數據 //如果數組中有排序字段則先排序 返回排序后的數組 if(array_key_exists(self::$order, $items[0])) {//二維數組中存在排序的字段則進行排序 usort($items, array(__CLASS__, "compare"));//用戶自定義的函數或者方法排序(當前類的compare方法) } //取出id座位鍵名將該數據裝入$tmpMap(將下標值替換成typeid) foreach ($items as $item) { $tmpMap[$item[$this->id]] = $item; } //自定義開始:取出帶父類別名的類別名 if ($allpath) { foreach ($tmpMap as $key =>$temp) { //如果pid>0即存在父類別 if ($temp[$this->pid ]) { //拿到path $path=$temp[$this->path]; $pathArr=explode($this->ps, $path);//fuid數組 //取出父id數組的名字 $aname=''; foreach ($pathArr as $pid) { if ($pid!=0) { foreach ($tmpMap as $value) { if ($pid==$value[$this->id]) { $aname.=$value[$this->typename].'/'; } } }else{ continue; } } $aname= $aname.$temp[$this->typename]; }else{ $aname=$temp[$this->typename]; } $tmpMap[$key][$this->pathname]=$aname; } } //自定義結束 foreach ($items as $item) { if (isset($tmpMap[$item[$this->pid]])) { //循環原數組,原數組中的pid是臨時數組的id時,將原數組的這組數據插入到子數組中 $tmpMap[$item[$this->pid]][$this->son][] = &$tmpMap[$item[$this->id]]; } else { //如果pid=0時 $tree[] = &$tmpMap[$item[$this->id]]; } } return $this->pathchild($tree); } /** * 設置類路路徑, 和獲取全部子類 * return eg ["childs"] => string(4) "2,3," */ private function pathchild($arr, $path='') { $xarr = array(); foreach ($arr as $k=>$v) { $xarr[$k]=$v; $xarr[$k][$this->path] = $path.$this->ps.$v[$this->pid]; $xarr[$k][$this->childs] = ''; if(isset($xarr[$k][$this->son])) { $xarr[$k][$this->son]=$this->pathchild($xarr[$k][$this->son], $xarr[$k][$this->path]); foreach($xarr[$k][$this->son] as $vv) { $xarr[$k][$this->childs] .= $vv[$this->id]; $xarr[$k][$this->childs] .= $this->ps.$vv[$this->childs]; } } } return $xarr; } /** * * 返回帶有層數級別的二維數組 * @param array $arr 從表中獲取的數組 * @return array 處理過的數組 */ public function getList($arr, $allpath=false) { $data=$this->getTree($arr, $allpath); return $this->clevel($data); } /** * 轉多層數組為二維數組, 并加上層數組別 */ private function clevel($arr, $num=0) { $this->i = $num; //遍歷多維數組 foreach ($arr as $v) { if (isset($v[$this->son])) { //遍歷后存在子數組情況 $v[$this->level] = $this->i++;//depath $subcat = $v[$this->son];//將子數組臨時存放 unset($v[$this->son]);//刪除子數組 $v[$this->childs] = trim($v[$this->childs], $this->ps);//去除多余的分隔符 $v[$this->path] = trim($v[$this->path], $this->ps);//去除多余的分隔符 $this->narr[$v[$this->id]]=$v; $this->clevel($subcat, $this->i); } else { $v[$this->level] = $this->i; $v[$this->childs] = trim($v[$this->childs], $this->ps); $v[$this->path] = trim($v[$this->path], $this->ps); $this->narr[$v[$this->id]]=$v; } } $this->i--; return $this->narr; } /** * 內部使用方法, 將按二維數組中的指定排序字段排序 */ private static function compare($x,$y){ if($x[self::$order] == $y[self::$order]) { return 0; } elseif($x[self::$order] < $y[self::$order]){ return -1; } else { return 1; } } } ``` ``` $tree=new Org\LAMP\CatTree(); $tree->setId('partsid'); $tree->setPid('parentid'); $tree->setTypename('pname'); $list=$tree->getList($partsList,true); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看