0

I have a csv file that I would like to generate a summary report from. The csv looks like this :

enter image description here

The csv has in each row an activity and the coresponding time when it starts.

The summary I'm trying to generate has to look like this :

enter image description here

Basically I need to show each activity and the times when it starts and it ends

I did as following in PHP, I'm almost done but the result I get is not really what I want :

$csvFileName = "The csv path";
$report = array();
$file = fopen($csvFileName, "r");
while (($data = fgetcsv($file, 8000, "\n")) !== FALSE) {
    $num = count($data);
    for ($c = 0; $c < $num; $c++) {
        $t = explode(',', $data[$c]);
        $time = $t[0];
        $activity = $t[1];
    $report[] = array($activity, $time);
    }
}
fclose($file);
//I'm reading the whole file content and copying it into an array.
$summaryReport = array();
$j = 1;
for($i=0; $i<sizeof($report); $i++){
  if($report[$i][0] !== $report[$j][0]){
   array_push($summaryReport,array($report[$i][0],$report[$i][1],$report[$j][1]));
  }
  $j++;
}
echo json_encode($summaryReport);

The output json looks like this :

[["Start","10:42","10:59"],["Driving route","11:10","11:50"],["Lunch-Rest Break","11:50","11:57"],["Driving route","11:57","12:03"],["Break","12:11","12:41"],["Driving route","13:05","14:09"],["Waiting","14:14","14:28"]]

What I'm looking for as result is something like that:

[["Start","10:42","10:59"],["Driving route","10:59","11:50"],["Lunch-Rest Break","11:50","11:57"],["Driving route","11:57","12:03"],["Break","12:03","12:41"],["Driving route","12:41","14:09"],["Waiting","14:09","14:28"],["End","14:28"]]

my coding logic is not really working well, does anyone see how can I do a simple loop to do what I'm looking for?

Thank you in advance.

1
  • 1
    This can't work: summaryReport variable spelled without $. No semicolon after $report = array() Commented Feb 8, 2014 at 3:52

1 Answer 1

1

The result can be achieved much easier. Look at my code, I got rid of all your inner loops, fixed syntax errors and there is no need to store the whole csv file in memory:

PHP code

<?php
$csvFileName = "./test.csv";
$file = fopen($csvFileName, "r");
$summaryReport = array();

$i = 0;
$previous_name = null;
while ($data = fgetcsv($file, 8000)) {
    if ($previous_name !== $data[1])
    {
        $summaryReport[$i] = array($data[1], $data[0]);
        if ($i > 0)
        {
            $summaryReport[$i-1][2] = $data[0];
        }
        $previous_name = $data[1];
        ++$i;
    }
}
fclose($file);

echo json_encode($summaryReport);

Test csv file

10:41,Start
10:59,Driving
11:29,Driving
11:11,End

Output

[["Start","10:41","10:59"],["Driving","10:59","11:11"],["End","11:11"]]
Sign up to request clarification or add additional context in comments.

4 Comments

Hello, thanks for your answer however the json I get using your code is like that : '[["Start","10:42","10:59"],["Driving route","10:59","11:10"],["Driving route","11:10","11:50"],["Lunch-Rest Break","11:50","11:57"],["Driving route","11:57","12:03"],["Break","12:03","12:11"],["Break","12:11","12:41"],["Driving route","12:41","13:05"],["Driving route","13:05","14:09"],["Waiting","14:09","14:14"],["Waiting","14:14","14:28"],["End","14:28"]]'
What I'm looking for is a result like that: '[["Start","10:42","10:59"],["Driving route","10:59","11:50"],["Lunch-Rest Break","11:50","11:57"],["Driving route","11:57","12:03"],["Break","12:03","12:41"],["Driving route","12:41","14:09"],["Waiting","14:09","14:28"],["End","14:28"]]'
Your code is just reading the csv line by line and printing the time , what I want is to get the time when an activity starts and ends for instance I have activity "Driving route" that starts at "10:59" but ends at "11:50" so the summary for this activity should be ["Driving route","10:59","11:50"]
@OussamaLord Hi, just read your message. Will try to fix the code soon.

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.