天天看点

将数组转化为键值对

public 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/rickons/p/5368208.html