0

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,

  1. first_time_stamp
  2. SIP/DIP
  3. sum_of_all_values_at_SIP
  4. 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!!

1
  • Have you taken a look at fgetcsv()? Might be helpful in your situation... Just a thought. Commented Nov 9, 2014 at 7:57

1 Answer 1

1

See if this works for you. As @jerdiggity wrote, you can also use fgetcsv(), but in your case (no quotes, and in particular no quoted commas or newlines in your CSV data), it's not necessary.

$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] = 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++;  
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.