批量更新
public function updateBatch($tableName = "", $multipleData = array()){
if( $tableName && !empty($multipleData) ) {
// column or fields to update
$updateColumn = array_keys($multipleData[0]);
$referenceColumn = $updateColumn[0]; //e.g id
unset($updateColumn[0]);
$whereIn = "";
$q = "UPDATE ".$tableName." SET ";
foreach ( $updateColumn as $uColumn ) {
$q .= $uColumn." = CASE ";
foreach( $multipleData as $data ) {
$q .= "WHEN ".$referenceColumn." = \"".$data[$referenceColumn]."\" THEN '".$data[$uColumn]."' ";
}
$q .= "ELSE ".$uColumn." END, ";
}
foreach( $multipleData as $data ) {
$whereIn .= "'".$data[$referenceColumn]."', ";
}
$q = rtrim($q, ", ")." WHERE ".$referenceColumn." IN (". rtrim($whereIn, ', ').")";
// Update
$result = DB::update(DB::raw($q));
$data = [
'status' => 200,
'message' => '成功更新'.$result.'条',
] ;
return $data;
} else {
$data = [
'status' => 400,
'message' => '更新失败',
] ;
return $data;
}
}
去掉二维数组里重复项
function array_unique_bykey($arr, $key){
$tmp_arr = array();
foreach($arr as $k => $v)
{
if(in_array($v[$key], $tmp_arr))
{
unset($arr[$k]);
}
else {
$tmp_arr[$k] = $v[$key];
}
}
return $arr;
}
读取csv
这个读取完,会自动做成key - value的数据,就不用每次用[0], [1]来访问字段了。
function readCsv($filename){
ini_set("auto_detect_line_endings", true);
$rows = [];
if ($csvFileHandler = fopen($filename, 'r')) {
$columns = fgetcsv($csvFileHandler, 0, ",");
while (($rowData = fgetcsv($csvFileHandler, 0, ",")) !== false) {
foreach ($columns as $columnIndex => $columnName) {
if(empty($columnName)) continue;
$row[$columnName] = $rowData[$columnIndex];
}
$rows[] = $row;
}
}
return $rows;
}
导出csv
function outputCsv($data, $filename, $emptyString = 'No Data'){
if($data==false || count($data)==0){
echo $emptyString;
exit;
}
header('Content-Type: text/csv; charset=UTF-8');
header("Content-Disposition: attachment; filename={$filename}.csv");
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
ob_start();
$fp = fopen( 'php://output', 'w' ) or die();
fputcsv($fp, array_keys(reset($data)));
foreach ($data as $dataRow){
fputcsv($fp, array_values($dataRow));
}
@fclose($fp);
$output = ob_get_contents();
ob_end_clean();
echo "\xEF\xBB\xBF" . $output;
exit;
}
public function myCurl($url, $header = null, $proxy = null, $method = "GET", $options = array()){
$curl = curl_init();
//基础参数
$defaultOptions = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
);
//额外指定参数
$options = $defaultOptions + $options;
//设置代理
if(!empty($proxy)){
$options[CURLOPT_PROXY] = $proxy['proxy'];
$options[CURLOPT_PROXYPORT] = $proxy['port'];
//$options[CURLOPT_PROXYTYPE] = $proxy['type'];
}
//CURLOPT_HTTPHEADER
if(!empty($header)){
$options[CURLOPT_HTTPHEADER] = $header;
}
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
//$info = curl_getinfo($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
return $response;
}
}