title: Building a "crosstab" or "pivot" table from an array in php
body: <p>I have an array of objects defined similarly to the below:</p>
  
  <pre><code>$scores = array();
  
  // Bob round 1
  $s = new RoundScore();
  $s-&gt;Round_Name = 'Round 1';
  $s-&gt;Player_Name = 'Bob';
  $s-&gt;Score = 10;
  $scores[0] = $s;
  
  // Bob round 2
  $s = new RoundScore();
  $s-&gt;Round_Name = 'Round 2';
  $s-&gt;Player_Name = 'Bob';
  $s-&gt;Score = 7;
  $scores[1] = $s;
  
  // Jack round 1
  $s = new RoundScore();
  $s-&gt;Round_Name = 'Round 1';
  $s-&gt;Player_Name = 'Jack';
  $s-&gt;Score = 6;
  $scores[2] = $s;
  
  // Jack round 2
  $s = new RoundScore();
  $s-&gt;Round_Name = 'Round 2';
  $s-&gt;Player_Name = 'Jack';
  $s-&gt;Score = 12;
  $scores[3] = $s;
  </code></pre>
  
  <p>If I loop through and dump the <code>$scores</code> object into a table, it will look something like this:</p>
  
  <pre>
  Round_Name   Player   Score
  ----------------------------
  Round 1      Bob        10
  Round 2      Bob         7
  Round 1      Jack        6
  Round 2      Jack       12
  </pre>
  
  <p>What I want, however, is something like this:</p>
  
  <pre>
  Player  Round 1  Round 2  Total
  -------------------------------
  Bob       10        7       17
  Jack       6       12       18
  </pre>
  
  <p>I'm not going to know in advance how many rounds or players there'll be and let's just say I can't change the way the objects are constructed.</p>
  
  <p>What's the most efficient way to do this in php?</p>