This callback is a little difficult! Create an infinite level classification tree based on an array

public function test(){
        $data = array(
            ""=>array(
                ""=>array(
                    ""=>array(
                        "ysy"=>1,
                        "tsm"=>9
                    ),
                    ""=>array(
                        "ysy"=>2,
                        "tsm"=>8
                    )
                ),
                ""=>array(
                    ""=>array(
                        ""=>3,
                        ""=>6,
                    ),
                    ""=>array(
                        ""=>4,
                        ""=>5,
                    ),
                )
            )
        );
       $result = $this->initTree($data,0,1,1);
    }
    /**
     * 
     * @param $data
     * @param $level
     * @param int $step
     * @param array $arr
     * @return int
     */
    protected function initTree($data,$pid,$level=1,$step=1,$arr=[])
    {
            foreach ($data as $k=>$v) {
                //
                $sql = "select id from srv_kit_category where pid=".$pid." and name="" . $k.""";
                $idObj = $this->dao->querySql($sql);
                $id = $idObj?$idObj[0]->id:0;

                //id
                if (!$id) {
                    $insertData = array(
                        "pid" => $pid,
                        "name" => $k,
                        "level"=>$level,
                    );
                    $this->dao->insert("srv_kit_category")->data($insertData)->check("name", "notempty")->exec();
                    if (dao::isError()) {
                       return 404;
                    }
                    $id = $this->dao->lastInsertID();//id
                }

                //idlevel
                if (is_array($v)) {
                    $arr[$step]=$id;
                    $pid = $arr[$step];
                    $stepPP;
                    $levelPP;
                }
                $this->initTree($v, $pid,$level,$step,$arr);//
                $step--;
                $level--;
            }
            return 200;

    }

the result is as follows




Php
Mar.15,2021

Menu