简体   繁体   中英

PHP CSV - indeterminate rows to columns

I have a CSV file of clients with feature names and a (Metadata) json string that defines that feature. Each client can have anywhere between 10 to 15 features, and there's no way to be sure which client has what.

Visually in this format:

Client Feature Metadata
client1 featureName1 jsonstring
client1 featureName2 jsonstring
client1 featureName3 jsonstring
client2 featureName1 jsonstring
client2 featureName2 jsonstring
client2 featureName3 jsonstring

With this data, I need to create a readable format of the json for stakeholders to understand how the features are being used by our clients. Manually, I created a spreadsheet with each client on a single row with the metadata in columns. This isn't maintainable, considering it took me a solid weekend to do, and the number of clients is rising.

Now I'm looping through the CSV file to create columns for each client from the metadata; but I don't know each feature that a client will have, so I can't dynamically build a table with matching header row. Here's what I've started building, and you can see in the logic that if a feature doesn't exist then the data won't match the header:

<table>
    <thead>
        <tr>
            <td>Client</td>
            <td>adminNotifications</td>
            <td>indexerJobSync - default</td>
            <td>indexerJobSync - defined</td>
        </tr>
    </thead>
    <tbody>
        <tr><?php
            $file = fopen("query.csv", "r");
            fgetcsv($file); //skip first row
            while (!feof($file)) {
                $data = fgetcsv($file, null, ';');
                $meta = json_decode($data[3], TRUE);
                if ($data[0] === 'BillyBobsCrabShack') {
                    if ($data[1] === 'adminNotifications') echo "<td>" . $meta['email'] . "</td>";
                    if ($data[1] === 'indexerJobSync') echo "<td>" . $meta['defaultIndexer'] . "</td><td>" . $meta['definedIndexer'] . "</td>";
                }
            }
            fclose($file);
            ?></tr>
    </tbody>
</table>

I ended up writing logic to create an array that I can work with first, and display second. In my situation, the number of features is finite but they're not enabled across all clients, so I created an array of features with clients assigned to them.

$file = fopen("query.csv", "r");
  fgetcsv($file); //skip first row
  $newdata = [];
  while (!feof($file)) {
    $data = fgetcsv($file, null, ';');
    $newdata[$data[1]][$data[0]] = $data[3];
  }
  fclose($file);

And this creates an array such as:

Array
(
    [feature1] => Array
        (
            [client1] => jsonstring
            [client2] => jsonstring
            [client3] => jsonstring
        )

    [feature2] => Array
        (
            [client4] => jsonstring
            [client5] => jsonstring
            [client6] => jsonstring
        )
)

From there, I just write the array keys into my display, or however I need to handle it:

<?= isset($newdata['fileCategory']['client1']) ? "TRUE":"FALSE" ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM