title: Create a 'pivot' table from array
body: <p>I have an array that looks like this:</p>
  <pre><code>Array (
      Array ( [date] =&gt; 04/2021 [name] =&gt; Fred [value] =&gt; 12.00 )
      Array ( [date] =&gt; 04/2021 [name] =&gt; Tom [value] =&gt; 160.00 )
      Array ( [date] =&gt; 04/2021 [name] =&gt; Mike [value] =&gt; 9.00 )
      Array ( [date] =&gt; 07/2021 [name] =&gt; Tony [value] =&gt; 200.00 )
      Array ( [date] =&gt; 07/2021 [name] =&gt; Fred [value] =&gt; 43.00 )
      Array ( [date] =&gt; 07/2021 [name] =&gt; Tom [value] =&gt; 114.00 )
      Array ( [date] =&gt; 07/2021 [name] =&gt; Mike [value] =&gt; 28.00 )
  )
  </code></pre>
  <p>I am trying to get the data into a simple HTML table where the name goes on the x axis, the date on the y axis and the values where they intersect.</p>
  <p>For example:</p>
  <pre><code>          Fred     Tom     Mike    Tony
  04/2021   12.00    160.00  9.00    0
  07/2021   43.00    114.00  28.00   200.00
  </code></pre>
  <p>I do not know if I am over thinking this but I cannot get this to work - I can get the x axis to work by pulling out the unique values for name but then when it comes to the y axis and the intersections I cannot get the data to display how I need.</p>
  <p>I either end up with repetition of columns or a single column and so on. This is where I have gotten up to but still scratching my head how to do this:</p>
  <pre><code>&lt;?php
  $unique_names = array_unique(array_map(function($elem){
      return $elem['name'];
  }, $data));
  ?&gt;
  
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;&amp;nbsp;&lt;/th&gt;
          &lt;?php foreach ($unique_names as $i) { ?&gt;
              &lt;th&gt;&lt;?php print $i;?&gt;&lt;/th&gt;
          &lt;?php } ?&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  
  &lt;tbody&gt;
  &lt;?php
  foreach ($data as $row) {
      for ($i=0; $i&lt;=count($unique_names);$i++) {
          if ($i==0) {
              print &quot;&lt;tr&gt;&lt;td&gt;&quot;.$row['date'].&quot;&lt;td&gt;&quot;;
          } else {
              print &quot;&lt;td&gt;&quot;.$row['value'].&quot;&lt;td&gt;&quot;;
          }
  
          if ($i==count($unique_dates)) {
              print &quot;&lt;/tr&gt;&quot;;
          }
      }
  }
  ?&gt;
  &lt;/tbody&gt;
  </code></pre>