php add new fields to the array and display it -
php add new fields to the array and display it -
the code given below read csv file , retrieve values fields $time,$sip,$dip , $data. need retrieve,
first_time_stamp sip/dip sum_of_all_values_at_sip sum_of_all_values_at_dipbelow code total sum up. can't find sip , dip separately. need find sip , dip separately. first time stamp.
$lines =file('/ghgj.csv'); $records=array(); $i=1; foreach($lines $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 illustration of csv file 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 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!!
see if works you. @jerdiggity wrote, can utilize fgetcsv(), in case (no quotes, , in particular no quoted commas or newlines in csv data), it's not necessary.
$lines =file('/ghgj.csv'); $records=array(); $i=1; foreach($lines $line) { list($time,$sip,$dip,$data)= explode(',',$line); if(substr($sip, 0, 3 )=="10.") { if (!key_exists($sip, $records)) { $records[$sip] = array( 'timestamp' => $time, 'sip' => 0, 'dip' => 0 ); } $records[$sip]['sip'] += $data; } else if(substr($dip, 0, 3 )=="10." ) { if (!key_exists($dip, $records)) { $records[$dip] = array( 'timestamp' => $time, 'sip' => 0, 'dip' => 0 ); } $records[$dip]['dip'] += $data; } else { continue; } $i++; }
php arrays csv multidimensional-array
Comments
Post a Comment