array_to_sql

function array_to_sql($array, $type='insert', $exclude = array()){
    $sql = '';
    if(count($array) > 0){
        foreach ($exclude as $exkey) {
            unset($array[$exkey]);//剔除不要的key
        }
    
        if('insert' == $type){
            $keys = array_keys($array);
            $values = array_values($array);
            $col = implode("`, `", $keys);
            $val = implode("', '", $values);
            $sql = "(`$col`) values('$val')";
        }else if('update' == $type){
            $tempsql = '';
            $temparr = array();
            foreach ($array as $key => $value) {
                $tempsql = "'$key' = '$value'";
                $temparr[] = $tempsql;
            }
            $sql = implode(",", $temparr);
        }
    }
    return $sql;
}
原文地址:https://www.cnblogs.com/wanghaitao/p/9440322.html