Login light

leetcode/menu生成.md

<?php
$input = '[{"id":1,"pid":0},{"id":2,"pid":1},{"id":4,"pid":2},{"id":10,"pid":2},{"id":5,"pid":10},{"id":6,"pid":0},{"id":7,"pid":10},{"id":128,"pid":7}]';
class dealMenu{
    function __construct($jsonStr){
        $this->jsonStr = $jsonStr;
    }
    function loadJson(){
        $this->jsonObjById = [];
        $this->jsonObjByPid = [];
        foreach(json_decode($this->jsonStr,1) as $arr){
            // $arr->childItem = [];
            $this->jsonObjById[$arr['id']] = $arr;

            if(0 != $arr['pid']){
                if(isset($this->jsonObjByPid[$arr['pid']])){
                    $this->jsonObjByPid[$arr['pid']][] = $arr;
                }else{
                    $this->jsonObjByPid[$arr['pid']] = [$arr];
                }
            }
                
        }
        $this->result = [];
    }
    /**
     * 是否为叶节点
     */
    function ifEnd($id){
        if(array_key_exists($id, $this->jsonObjByPid)){
            return false;
        }else{
            return true;
        }
    }
    /**
     * 当前节点是否可以收起
     */
    function ifCanMakeChild($pid){
        $ret = true;
        foreach($this->jsonObjByPid[$pid] as $nodeObj){
            if(!$this->ifEnd($nodeObj['id'])){
                $ret = false;
                break;
            }
        }
        return $ret;
    }
    /**
     * make child
     */
    function makeChild($pid){
        //make child
        $this->jsonObjById[$pid]['childItem'] = [];
        foreach($this->jsonObjById as $obj){
            if($obj['pid'] === $pid){
                $this->jsonObjById[$pid]['childItem'][] = $this->jsonObjById[$obj['id']];
                unset($this->jsonObjById[$obj['id']]);
            }
        }
        unset($this->jsonObjByPid[$pid]);
    }

    /**
     * 从后到前遍历
     */
    function makeMenuTail2Head(){
        //直到所有的item都没有父目录,结束
        while(true){
            $front_count = count($this->jsonObjById);
            foreach($this->jsonObjById as $arrItem){
                //叶子节点 很多冗余判断
                if(0 !== $arrItem['pid'] && $this->ifEnd($arrItem['id'])){
                    //兄弟节点是否都为叶子节点
                    if($this->ifCanMakeChild($arrItem['pid'])){
                        $this->makeChild($arrItem['pid']);
                        break;
                    }
                }
            }
            if(count($this->jsonObjById) === $front_count){
                break;
            }
        }
        $this->result = $this->jsonObjById;
    }

    function getResult(){
        if(empty($this->result)){
            $this->makeMenuTail2Head();
        }
        return json_encode($this->result);
    }
}
if(!empty($_POST['inputJson'])){
    $arr = new dealMenu($_POST['inputJson']);
    $arr->loadJson();
    $arr->makeMenuTail2Head();
    $output = $arr->getResult();
}
if(isset($_GET['code'])){
    show_source($_SERVER['SCRIPT_FILENAME']);
    exit;
}
?>
<style>
textarea{
    width: 100%;
    height: 120px;
}
</style>
<meta data-n-head="true" data-hid="charset" charset="utf-8">
<form action="" method="post">
json源文:
<textarea name="inputJson"><?=$input?></textarea>
<input type="submit" value="生成目录" />
<textarea><?=$output?></textarea>
</form>
<button onclick="window.location='?code=1';">查看源码</button>
```