0

I have googled to do this but I think I am lacking something to make it work and after the merging I will save it to csv file or mysql db.

This is the sample data:

01 2015-09-03 08:01
01 2015-09-03 11:03
01 2015-09-03 13:15
01 2015-09-03 17:12
07 2015-09-03 08:15
07 2015-09-03 17:06
01 2015-09-04 08:05
01 2015-09-04 11:03

I want the resulting array to be like this before saving it to a csv file:

01 | 2015-09-03 | 08:01 | 11:03 | 13:15 | 17:12
07 | 2015-09-03 | 08:15 | 17:06
01 | 2015-09-04 | 08:05 | 11:03

creating a header based on the number of elements it have. I tried this code:

foreach($line as $value){
fputcsv($converted,$value,"\t");//write the new array to csv
}
5
  • The one that I posted the foreach loop. Commented Sep 4, 2015 at 5:50
  • are date time and id three different columns? Commented Sep 4, 2015 at 5:51
  • yes its a multidimensional array. Commented Sep 4, 2015 at 5:52
  • Post your array structure instead along with expected output Commented Sep 4, 2015 at 5:54
  • the sample data was the array structure having duplicates in ID and date. Commented Sep 4, 2015 at 5:56

2 Answers 2

1

You can use the explode function to convert your string to an array:

$categories = [];

foreach($lines as $line) {
  $array = explode(' ', $line);

  // Then you can store your data using column 1 and 2 as a key
  $key = $array[0] . $array[1];
  $categories[$key][] = $array;
}

// finally, you can go through the $categories array
foreach ($categories as $category) {
   $csv = [$category[0][0], $category[0][1]];
   foreach ($category as $value) {
     $csv[] = $value;
   }
   fputcsv($handle, $csv);
}

The naming of the vars is terrible but I hope you get the idea :)

Sign up to request clarification or add additional context in comments.

1 Comment

I will try the code and it looks like it will solve my problem.
0

Try this one.

<?php
$lines = "
01 2015-09-03 08:01
01 2015-09-03 11:03
01 2015-09-03 13:15
01 2015-09-03 17:12
07 2015-09-03 08:15
07 2015-09-03 17:06
01 2015-09-04 08:05
01 2015-09-04 11:03
";
$line_array = explode("\n", $lines);

$csv_line_array = array();
$csv = 0;
$time = "";
for ($i = 0; $i < sizeof($line_array); $i++) {
    if ($i < (sizeof($line_array) - 1)) {
        if (substr($line_array[$i], 0, 13) == substr($line_array[$i + 1], 0, 13)) {
            $time = $time . " | " . substr($line_array[$i + 1], 14, 5);
            $csv_line_array[$csv] = $line_array[$i] . " | " . $time;
            $csv++;
        } else {
            $time = substr($line_array[$i + 1], 14, 5);
            $csv_line_array[$csv] = $line_array[$i];
        }
    }
}
$csv_line_array2 = array();
for ($i = 0; $i < sizeof($csv_line_array); $i++) {
    if ($i < (sizeof($csv_line_array) - 1)) {
        if (substr($csv_line_array[$i], 0, 13) == substr($csv_line_array[$i + 1], 0, 13)) {
            if (strlen($csv_line_array[$i]) < strlen($csv_line_array[$i + 1]))
                unset($csv_line_array[$i]);
            else
                $csv_line_array[$i] = $csv_line_array[$i + 1];
        }
    }

}

/*if (strlen($csv_line_array[sizeof($csv_line_array)]) < strlen($csv_line_array[sizeof($csv_line_array) - 1]))
    $csv_line_array[sizeof($csv_line_array)] = $csv_line_array[sizeof($csv_line_array) - 1];
else
    unset($csv_line_array[$i]);*/

var_dump($line_array);
var_dump($csv_line_array);
?>

will output something like this:

array (size=10)
  0 => string '' (length=0)
  1 => string '01 2015-09-03 08:01' (length=19)
  2 => string '01 2015-09-03 11:03' (length=19)
  3 => string '01 2015-09-03 13:15' (length=19)
  4 => string '01 2015-09-03 17:12' (length=19)
  5 => string '07 2015-09-03 08:15' (length=19)
  6 => string '07 2015-09-03 17:06' (length=19)
  7 => string '01 2015-09-04 08:05' (length=19)
  8 => string '01 2015-09-04 11:03' (length=19)
  9 => string '' (length=0)
array (size=4)
  2 => string '01 2015-09-03 13:15 | 08:01 | 11:03 | 13:15 | 17:12' (length=51)
  3 => string '07 2015-09-03 08:15 | 08:15 | 17:06' (length=35)
  4 => string '01 2015-09-04 08:05 | 08:05 | 11:03' (length=35)
  5 => string '01 2015-09-04 11:03' (length=19)

1 Comment

I will save it to a csv file can I save it like the second array?

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.