title: PHP CSV - indeterminate rows to columns
body: <p>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.</p>
  <p>Visually in this format:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Client</th>
  <th>Feature</th>
  <th>Metadata</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>client1</td>
  <td>featureName1</td>
  <td>jsonstring</td>
  </tr>
  <tr>
  <td>client1</td>
  <td>featureName2</td>
  <td>jsonstring</td>
  </tr>
  <tr>
  <td>client1</td>
  <td>featureName3</td>
  <td>jsonstring</td>
  </tr>
  <tr>
  <td>client2</td>
  <td>featureName1</td>
  <td>jsonstring</td>
  </tr>
  <tr>
  <td>client2</td>
  <td>featureName2</td>
  <td>jsonstring</td>
  </tr>
  <tr>
  <td>client2</td>
  <td>featureName3</td>
  <td>jsonstring</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>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.</p>
  <p>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:</p>
  <pre><code>&lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;td&gt;Client&lt;/td&gt;
              &lt;td&gt;adminNotifications&lt;/td&gt;
              &lt;td&gt;indexerJobSync - default&lt;/td&gt;
              &lt;td&gt;indexerJobSync - defined&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;&lt;?php
              $file = fopen(&quot;query.csv&quot;, &quot;r&quot;);
              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 &quot;&lt;td&gt;&quot; . $meta['email'] . &quot;&lt;/td&gt;&quot;;
                      if ($data[1] === 'indexerJobSync') echo &quot;&lt;td&gt;&quot; . $meta['defaultIndexer'] . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $meta['definedIndexer'] . &quot;&lt;/td&gt;&quot;;
                  }
              }
              fclose($file);
              ?&gt;&lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>