0
下面給出的代碼是讀取csv文件並將值檢索到字段$ time,$ sip,$ dip和$ data。我需要檢索,php將新字段添加到數組並將其顯示
- first_time_stamp
- SIP/DIP
- sum_of_all_values_at_SIP
- sum_of_all_values_at_DIP
下面的代碼是總和起來。但我找不到分開的啜飲。我需要分開尋找啜飲和浸泡。也是第一次的時間戳。
2014-10-31 23:34:06,10.101.11.122,54.252.136.82,2047
2014-10-31 23:34:08,31.13.70.81,10.101.84.6,49580
2014-10-31 23:34:15,10.101.11.122,54.252.136.82,20
2014-10-31 23:34:09,54.252.136.82,10.101.11.122,20
2014-10-31 23:34:12,10.101.11.13,10.101.11.122,20
如:
$lines =file('/ghgj.csv');
$records=array();
$i=1;
foreach($lines as $line)
{
list($time,$sip,$dip,$data)= explode(',',$line);
if(substr($sip, 0, 3)=="10.")
{
if(key_exists($sip, $records))
{
$records[$sip] += $data;
}
else
{
$records[$sip] = $data;
}
}
else if(substr($dip, 0, 3)=="10.")
{
if(key_exists($dip, $records))
{
$records[$dip] += $data;
}
else
{
$records[$dip] = $data;
}
}
else
{
continue;
}
$i++;
}
}
CSV文件的例子在下面給出我的代碼輸出:
10.101.11.122(sip or dip),2087
10.101.84.6,49580
10.101.11.13,20
如:輸出應該是這樣的:
2014-10-31 23:34:06(timestamp),10.101.11.122(common sip or dip),2067(sum of sip),20(sum of dip)
2014-10-31 23:34:08,10.101.84.6,0,49580
2014-10-31 23:34:06,10.101.11.13,20,0
謝謝!
你看過[fgetcsv()](http://php.net/manual/en/function.fgetcsv.php)嗎?可能對你的情況有所幫助......只是一個想法。 – jerdiggity 2014-11-09 07:57:22