首页 > 解决方案 > 基于键的分组值

问题描述

在此处输入图像描述 根据上图,我有一个棘手的输出,我需要像这样生成 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();
}

感谢你的帮助

标签: phplaravellaravel-excel

解决方案


你可以试试下面的代码。

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();

推荐阅读