php - 基于键的分组值
问题描述
根据上图,我有一个棘手的输出,我需要像这样生成 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();
}
感谢你的帮助
解决方案
你可以试试下面的代码。
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();
推荐阅读
- sass - 如何在 SASS 中检查地图的索引?
- mysql - 如何对现有 MySQL 数据库进行分片
- google-cloud-dataprep - 自 6/20 更新以来运行超过 72 小时的 Dataprep 作业。作业状态读取完成但未发布
- json - Ssis 部署
- java - 基于 URL 动态创建新的套接字
- google-cloud-platform - 从另一个 GCP 项目访问 Cloud SQL
- android - EMDK 串行通信找不到端口
- python - 如何在 Jupyter Notebook 中显示带有小箭头的缩进数?
- android-studio-3.1 - 无法解析“:app@debug/compileClasspath”的依赖关系:找不到任何与 com.android.support:appcompat-v7:29 匹配的版本。+
- php - 如何在 Wordpress 主页 php 模板中实现 Visual Composer VC 代码?