Reputation: 135
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
}
Upvotes: 0
Views: 71
Reputation: 823
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)
Upvotes: 0
Reputation: 1947
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 :)
Upvotes: 1