根据上面的图片,我得到了一个棘手的输出,我需要生成这样的CSV文件
Date,Reservoir Level,P5,P6,P2,P3,P4,SP6,SP4,SP5,SP3,SP2
1574043963000,19.1,,,,,,,,,,
1574039698000,20.665,11.722,,,,,,,,,
1574039699000,8.735,8.879,8.835,,,,,,,,
1574039702000,15.106,,,,,,,,,,
1574039703000,undefined,12.09,,,,,,,,,
1574039704000,13.707,,,,,,,,,,
1574039705000,13.17,,,,,,,,,,
...
我用Laravel-Excel生产它。这是我到目前为止所拥有的(基于下面给出的答案)
public function collection()
{
$rows = collect();
foreach ($this->data as $recordKey => $record) {
$date = $record->date;
$instrumentReading = json_decode($record->instrument_reading, true);
if ($rows->has($date)) {
$row = $rows->get($date);
} else {
$row = collect([
'date' => $date,
]);
}
$i = 0;
foreach ($instrumentReading as $key => $value) {
$elementIndex = array_keys($this->header, $key);
$price = $elementIndex[0];
$row->put($key, $value);
// info($price, [$i]);
++$i;
}
info($row);
$rows->put($date, $row);
}
return $rows;
}
我的问题从第二个条目P5开始:20.665进入Reservoir level
列。这之后是第三个条目P2:8.735也进入Reservoir level
列。我该如何解决?
已编辑
这是我设置标题的方式
public function headings(): array
{
$header = collect(['Date']);
foreach ($this->data as $rowNo => $row) {
$reading = json_decode($row->instrument_reading);
foreach ($reading as $ra1 => $val) {
if (!$header->contains($ra1)) {
$header->push($ra1);
}
}
}
$this->header = $header->toArray();
return $header->toArray();
}
感谢您的帮助
答案 0 :(得分:0)
如果我理解正确,您想根据日期合并所有行吗?
如果是的话,我认为类似的事情可能就是您所追求的:
public function collection()
{
$rows = collect();
foreach ($this->data as $recordKey => $record) {
$date = Carbon::createFromFormat('Y-m-d', $record->iv_date)->format('m/d/Y');
$instrumentReading = json_decode($record->instrument_reading, true);
if ($rows->has($date)) {
$row = $rows->get($date);
} else {
$row = collect([
'date' => $date
]);
}
foreach ($instrumentReading as $key => $value) {
$row->put($key, $value);
}
$rows->put($date, $row);
}
return $rows;
}
答案 1 :(得分:0)
您可以尝试以下代码。
function collection()
{
$rows = [];
$data = [
['date' => 123123, 'instrument_reading' => json_encode(['Reservoir Level' => '19.10'])],
['date' => 123123, 'instrument_reading' => json_encode(['P5' => '20.665', 'P6' => '11.722'])],
['date' => 123123, 'instrument_reading' => json_encode(['P2' => '8.735', 'P3' => '8.879', 'P4' => '8.835'])],
['date' => 123123, 'instrument_reading' => json_encode(['SP6' => '15.106'])],
['date' => 123123, 'instrument_reading' => json_encode(['SP4' => 'undefined', 'SP5' => '12.090'])],
['date' => 123123, 'instrument_reading' => json_encode(['SP3' => '13.707'])],
['date' => 123123, 'instrument_reading' => json_encode(['SP2' => '13.170'])],
];
foreach ($data as $record) {
$row = [];
$row['date'] = $record['date'];
foreach (json_decode($record['instrument_reading'], true) as $key => $value) {
$row[$key] = $value;
}
$rows[] = $row;
}
$keys = array_unique(array_merge_recursive(... array_map(function ($row) {
return array_values(array_keys($row));
}, $rows)));
foreach ($rows as &$row) {
foreach ($keys as $key) {
if (!array_key_exists($key, $row)) {
$row[$key] = null;
}
}
}
$handle = fopen('result.txt', 'w');
fputcsv($handle, $keys);
foreach ($rows as $r) {
fputcsv($handle, array_values($r));
}
fclose($handle);
}
collection();