您的位置:首页 > 其它

将数组转化为键值对

2016-04-08 15:10 190 查看
/**
* 将数组转化为键值对
* @param type $array
* @param type $type
* @param type $exclude
* @return type
*/
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: