The code given below is to read a csv file and retrieve the values to the fields $time,$sip,$dip and $data. I need to retrieve,
- first_time_stamp
- SIP/DIP
- sum_of_all_values_at_SIP
- sum_of_all_values_at_DIP
Below code is for the total sum up. But I can't find the sip and dip separately. I need to find sip and dip separately. Also the first time stamp.
$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++;
}
}
The example of csv file is given below:
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
Eg:Output of my code:
10.101.11.122(sip or dip),2087
10.101.84.6,49580
10.101.11.13,20
Eg:Output should Looks like:
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
Thank you!!