PHP 無限層級分類

最近看到一個博客有幾篇文章不錯,分享下。php

php 替代函數遞歸方式的無限層級分類生成樹方法 https://hanxv.cn/archives/98.htmlhtml

  
    
  
  
   
   
            
   
   
  1. ios

  2. git

  3. github

  4. 數組

  5. 微信

  6. 架構

  7. app

  8. composer

/**$data 節點list數據$parent_index 父節點索引名,默認值parent_id$data_index 各節點索引名,默認值id$child_name 樹狀結構中子節點位存於父節點的屬性值,默認值childhttps://github.com/AxiosCros/tpr-composer/blob/master/src/helper.php 根據節點id做爲key值單循環各節點,判斷是否有父節點如有父節點,則子節點綁定至父節點的child屬性中當出現a屬於b,b屬於c,c屬於a這種死循環所屬關係時,不會在$tree中體現*/ function tpr_infinite_tree($data,$parent_index='parent_id',$data_index='id',$child_name='child'){        $items = [];        foreach ($data as $d){            $items[$d[$data_index]] = $d;            if(!isset($d[$parent_index]) || !isset($d[$data_index]) || isset($d[$child_name])){                return false;            }        }        $tree = [];$n=0;        foreach($items as $item){            if(isset($items[$item[$parent_index]])){                $items[$item[$parent_index]][$child_name][] = &$items[$item[$data_index]];            }else{                $tree[$n++] = &$items[$item[$data_index]];            }        }        return $tree;    }echo '<pre>';    $data = [            ['id'=>1,'parent_id'=>0],           ['id'=>2,'parent_id'=>3],            ['id'=>3,'parent_id'=>1],           ['id'=>4,'parent_id'=>2],           ['id'=>5,'parent_id'=>6],           ['id'=>6,'parent_id'=>7],           ['id'=>7,'parent_id'=>5],       ];    print_r(tpr_infinite_tree($data));Array(    [0] => Array        (            [id] => 1            [parent_id] => 0            [child] => Array                (                    [0] => Array                        (                            [id] => 3                            [parent_id] => 1                            [child] => Array                                (                                    [0] => Array                                        (                                            [id] => 2                                            [parent_id] => 3                                            [child] => Array                                                (                                                    [0] => Array                                                        (                                                            [id] => 4                                                            [parent_id] => 2                                                        )                                                )                                        )                                )                        )                )        ))

php二維數組排序---支持單條件或多條件 https://hanxv.cn/archives/84.html

  
    
  
  
   
   
            
   
   
function arraySort($array,$sortRule,$order="asc"){    if(is_array($sortRule)){        /**         * $sortRule = ['book'=>"asc",'version'=>"asc"]; 條件支持N多個         */        usort($array, function ($a, $b) use ($sortRule) {            foreach($sortRule as $sortKey => $order){                if($a[$sortKey] == $b[$sortKey]){continue;}                return (($order == 'desc')?-1:1) * (($a[$sortKey] < $b[$sortKey]) ? -1 : 1);            }            return 0;        });    }else if(is_string($sortRule)){        /**         * $sortRule = "book";         * $order = "asc";         */        usort($array,function ($a,$b) use ($sortRule,$order){            if($a[$sortRule] == $b[$sortRule]){              return 0;            }            return (($order == 'desc')?-1:1) * (($a[$sortRule] < $b[$sortRule]) ? -1 : 1);        });    }    return $array;}$array = [                 ["book"=>10,"version"=>10],                 ["book"=>19,"version"=>30],                 ["book"=>10,"version"=>30],                 ["book"=>19,"version"=>10],                 ["book"=>10,"version"=>20],                 ["book"=>19,"version"=>20]];//單條件$array = arraySort($array,'book','asc');print_r($array);Array(    [0] => Array        (            [book] => 10            [version] => 10        )    [1] => Array        (            [book] => 10            [version] => 30        )    [2] => Array        (            [book] => 10            [version] => 20        )    [3] => Array        (            [book] => 19            [version] => 30        )    [4] => Array        (            [book] => 19            [version] => 10        )    [5] => Array        (            [book] => 19            [version] => 20        ))//多條件arraySort($array,['book'=>"asc",'version'=>"asc"]);print_r($array);Array(    [0] => Array        (            [book] => 10            [version] => 10        )    [1] => Array        (            [book] => 10            [version] => 30        )    [2] => Array        (            [book] => 10            [version] => 20        )    [3] => Array        (            [book] => 19            [version] => 30        )    [4] => Array        (            [book] => 19            [version] => 10        )    [5] => Array        (            [book] => 19            [version] => 20        ))

PHP 下載文件

  
    
  
  
   
   
            
   
   
           $file_name="test.mp3";            $mp3_url = "";            header( "Pragma: public" );            header('Expires: 0');            header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );            header( "Cache-Control: private", false );            header( "Content-Type: application/force-download ");            //header( "Content-Length: " . $fileSize);            header('Content-Disposition: attachment; filename="'.$file_name.'"');            header('Content-Transfer-Encoding: binary');            header('Connection: close');            readfile($mp3_url);            exit();

進制轉換https://hanxv.cn/archives/111.html

  
    
  
  
   
   
            
   
   
$uuid = uniqid('code');$resule = ConvertLogic::convert( $uuid, 16, 62);//輸出https://github.com/AxiosCros/tpr-cms/blob/master/library/logic/CodeLogic.php//string(17) "5a5c5b182386"//string(12) "7hoyVkRTi "

項目架構設計總結https://hanxv.cn/archives/81.html



本文分享自微信公衆號 - 蘇生不惑(susheng_buhuo)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索