yii2中判断数据表是否存在数据库中(原创)

分为两步:

第一步,找出数据库中所有表名,表名得到的是二维数组。

第二步,判断表名是否存在二维数组中

下面就贴我的代码咯。

   $table_name =‘table’;
        $juge = $handle->createCommand("show tables ")->queryAll();
//下面的deep_in_array()方法是自己写的方法,判断是否存在值是否存在二维数组中,yii2中调用本类方法,可以去掉action
$cun = $this->deep_in_array($table_name,$juge); if(!$cun){ echo json_encode("nodata"); return; }
   //判断二维数组是否存在值
    public  function deep_in_array($value, $array) {   
            foreach($array as $item) {   
                if(!is_array($item)) {   
                    if ($item == $value) {  
                        return true;  
                    } else {  
                        continue;   
                    }  
                }   
                   
                if(in_array($value, $item)) {  
                    return true;      
                } else if($this->deep_in_array($value, $item)) {  
                    return true;      
                }  
            }   
            return false;   
        }
原文地址:https://www.cnblogs.com/wenhainan/p/6029354.html