|
如果你有一套程序,用同时兼容mysql和sqlite两种数据库,或许以下代码对您有用。
PHP复制代码
if (! function_exists('sqlite')){
function sqlite ($arr,$foreach){
if ($foreach == 1){
if (! is_array($arr)) show_error ('所传递参数不是数组!');
foreach ($arr as $s){
$sqlite_return[] = set_sqlite_arr ($s);
}
return $sqlite_return;
}
else {
$arr = objectToArray ($arr);
$return_array = set_sqlite_arr ($arr);
$return_object = (object )$return_array;
return $return_object;
}
}
function set_sqlite_arr ($s)
{
$i = 0;
$count = count($s)-1;
$key = '';
$val = '';
foreach ($s as $k => $v){$i++;
if (! is_numeric($k)){
$field = explode('.',$k);
$value = (! isset($v))? '' : $v;
$key .= $field[1].sqlite_end ($i,$count);
$val .= $value.sqlite_end ($i,$count);
if ($i == $count) break;
}
}
$arr_key = explode(',',$key);
$arr_val = explode(',',$val);
$arr_sqlite = array_combine($arr_key,$arr_val);
return $arr_sqlite;
}
function sqlite_end ($i,$count)
{
if ($count == 0) show_error ('不能传递为0的参数!');
$return = ($i < $count)? ',':'';
return $return;
}
function objectToArray ($objParam)
{
$obj_param = (array)$objParam;
foreach ( $obj_param as $key=>$value ) {
if ( is_object($value) ) {
$this->objectToArray($value);
$obj_param[$key] = (array)$value;
}
if ( is_array($value) ) {
$this->objectToArray($value);
$obj_param[$key] = (array)$value;
}
}
return $obj_param;
}
}
复制代码 |
|