前两天帮公交公司处理ORACLE的数据问题的代码
<?php
error_reporting(1);
function get_timestr($str_time)
{
if(strpos($str_time,"AM"))
{
$str_time = str_replace("AM ",'',$str_time);
$time = strtotime($str_time);
}
else
{
$str_time = str_replace("PM ",'',$str_time);
$time = strtotime($str_time)+(12*60*60);
}
return $time;
}
$row = 0;
$handle = fopen("tmp004.csv","r");
$all_data = array();
$all_cart = array();
$TEMP_DATA['NEXT'] = '';
$TEMP_DATA['CURT'] = '';
$max_row = 23973;//$max_row = 23974;
while($data = fgetcsv($handle,1000,","))
{
if(!array_key_exists($data[4],$all_cart))
{
$all_cart[$data[4]] = array();
}
$all_data[] = $data;
$row++;
if($row==$max_row) break;
}
$row = 0;
while(1)
{
//echo 'CurrentData:';
$TEMP_DATA['CURT'] = current($all_data);
//echo $TEMP_DATA['CURT'][0]."\n";
//echo 'NextData:';
$TEMP_DATA['NEXT'] = next($all_data);
//echo $TEMP_DATA['NEXT'][0]."\n";
$temp_tiem = get_timestr($TEMP_DATA['NEXT'][2])-(60*60);
$cur_time = get_timestr($TEMP_DATA['CURT'][2]);
$in_fenzhong = $temp_tiem – $cur_time;
//echo 'CURT:'.$TEMP_DATA['CURT'][1].'NEXT:'. $TEMP_DATA['NEXT'][1]."\r\n"; //汽车跳变、状态跳变、或时间跳变结束本次累计
$same_cart = ($TEMP_DATA['CURT'][4]==$TEMP_DATA['NEXT'][4]);
$same_state = ($TEMP_DATA['CURT'][1]==$TEMP_DATA['NEXT'][1]);
//echo 'SAME_STATE:'.$same_state."\r\n";
if($in_fenzhong<=0 and $same_cart and $same_state )
{
//echo '都一样'."\r\n";
$is_in = false;
foreach($all_cart[$TEMP_DATA['CURT'][4]]['state'] as $state_temp_key=>$state_temp_value)
{
if($TEMP_DATA['CURT'][1]==$state_temp_value['StateID'] and $state_temp_value['END']==0)
{
$is_in = $state_temp_key;
}
}
//echo 'STATEID:'.$is_in."\n";
if($is_in===false) //新状态
{
//$last_iindex= count($all_cart[$TEMP_DATA['CURT'][4]]['state']);
//$all_cart[$TEMP_DATA['CURT'][4]]['state'][$last_iindex-1]['END'] = 1;
$all_cart[$TEMP_DATA['CURT'][4]]['state'][] =
array('StateID'=>$TEMP_DATA['CURT'][1],
'licheng'=> $TEMP_DATA['NEXT'][3]-$TEMP_DATA['CURT'][3],
'B_LINE' => $TEMP_DATA['CURT'][0].'|'.$TEMP_DATA['CURT'][1].'|'.$TEMP_DATA['CURT'][2].'|'.$TEMP_DATA['CURT'][33].'|'.$TEMP_DATA['CURT'][4].'|'.$TEMP_DATA['CURT'][5].'|'.$TEMP_DATA['CURT'][6],//到时后换为具体数据
'E_LINE' => $TEMP_DATA['NEXT'][0].'|'.$TEMP_DATA['NEXT'][1].'|'.$TEMP_DATA['NEXT'][2].'|'.$TEMP_DATA['NEXT'][3].'|'.$TEMP_DATA['NEXT'][4].'|'.$TEMP_DATA['NEXT'][5].'|'.$TEMP_DATA['NEXT'][6],//[0],//到时后换为具体数据
'END' => 0,
);
//echo '没有这个状态'.$TEMP_DATA['CURT'][1];
}else{
//echo '已经有'.$TEMP_DATA['CURT'][1]."这个状态了";
//echo $TEMP_DATA['NEXT'][0]."\n";
if($TEMP_DATA['NEXT'][0])
{
//echo 'CURT:'.$TEMP_DATA['CURT'][0].'NEXT:'. $TEMP_DATA['NEXT'][0]."\r\n";#p#分页标题#e#
if($TEMP_DATA['NEXT'][4]==$TEMP_DATA['CURT'][4] and $TEMP_DATA['NEXT'][1]==$TEMP_DATA['CURT'][1] )
{
//echo '是当前状态和车辆'.$is_in.'*';
$all_cart[$TEMP_DATA['CURT'][4]]['state'][$is_in]['E_LINE'] = $TEMP_DATA['NEXT'][0].'|'.$TEMP_DATA['NEXT'][1].'|'.$TEMP_DATA['NEXT'][2].'|'.$TEMP_DATA['NEXT'][3].'|'.$TEMP_DATA['NEXT'][4].'|'.$TEMP_DATA['NEXT'][5].'|'.$TEMP_DATA['NEXT'][6];//到时后换为具体数据
$all_cart[$TEMP_DATA['CURT'][4]]['state'][$is_in]['licheng'] += ($TEMP_DATA['NEXT'][3]-$TEMP_DATA['CURT'][3]);
}else{
//echo '不是当前状态和车辆'.$is_in.'*';
$all_cart[$TEMP_DATA['CURT'][4]]['state'][$is_in]['licheng'] += 0;
$all_cart[$TEMP_DATA['CURT'][4]]['state'][$is_in]['E_LINE'] = $TEMP_DATA['CURT'][0].'|'.$TEMP_DATA['CURT'][1].'|'.$TEMP_DATA['CURT'][2].'|'.$TEMP_DATA['CURT'][33].'|'.$TEMP_DATA['CURT'][4].'|'.$TEMP_DATA['CURT'][5].'|'.$TEMP_DATA['CURT'][6];//到时后换为具体数
}
}
}
}else{
//echo '不在一分钟内或不是同一辆车或不是同一个状态'."\r\n";
$last_iindex= count($all_cart[$TEMP_DATA['CURT'][4]]['state']);
if($all_cart[$TEMP_DATA['CURT'][4]]['state'][$last_iindex-1]['END']===0) $all_cart[$TEMP_DATA['CURT'][4]]['state'][$last_iindex-1]['END'] = 1;
}
$row++;
if($row==$max_row) break;
}
fclose($handle);
//print_r($all_data);
//print_r($all_cart);
$handle= fopen("data.csv","a");
$title = iconv("UTF-8","GB2312","汽车,状态ID,行驶里程,开始处数据,结束处数据\r\n");
fwrite($handle,$title);
//echo $title;
foreach($all_cart as $key=>$value)
{
/*
echo '汽车:'.$key."\r\n";
echo "\t状态ID \t里程 \t开始数据 \t结束数据";
echo "\r\n";
foreach($all_cart[$key]['state'] as $state_value)
{
echo "\t".$state_value['StateID'].','."\t".$state_value['licheng'].','."\t".$state_value['B_LINE'].','."\t".$state_value['E_LINE'];
echo "\r\n";
}*/
foreach($all_cart[$key]['state'] as $state_value)
{
$content =$key.','.$state_value['StateID'].','.$state_value['licheng'].','.$state_value['B_LINE'].','.$state_value['E_LINE']."\r\n";
fwrite($handle,$content);
//echo $content;
}
}
fclose($handle);
echo 'end ..';
?>