title: how to create a pivot array in php
body: <p>I  have the following :</p>
  
  <pre><code>   $data = [
    {model_num : "ABC", revision : "AA", "testRecipeID":85, value : 31.25, treatment : 'Pressure' },
    {model_num : "ABC", revision : "AA", "testRecipeID":85, value : 31.25, treatment : 'Gas' },
    {model_num : "ABC", revision : "AA", "testRecipeID":85, value : 33.12, treatment : 'Temp' },
    {model_num : "ABC", revision : "AA", "testRecipeID":85, value : 25.87, treatment : 'Current' },
    {model_num : "ABC", revision : "AB", "testRecipeID":86, value : 26.63, treatment : 'Pressure' },
    {model_num : "ABC", revision : "AB", "testRecipeID":86, value : 26.00, treatment : 'Gas' },
    {model_num : "ABC", revision : "AB", "testRecipeID":86, value : 23.75, treatment : 'Temp' }
  ];
  </code></pre>
  
  <p>and i would like to end up with something like this:</p>
  
  <pre><code>var data=[{model_num : "ABC", revision : "AA", "testRecipeID":85, "Pressure":31.25, "Gas":31.25, "Temp": 33.12,"Current":25.87 },{model_num : "ABC", revision : "AB", "testRecipeID":86, "Gas":26.00,"Temp":23.75}]
  </code></pre>
  
  <p>I know how to do this in JS but not on PHP and it turns out I need to do it in my php so that I can process the large amounts of data that I  have. I have this based on another question that I found but It doesn't work it returns a 0  </p>
  
  <pre><code>$table = array();
  $round_names = array();
  $total = array();
  
  foreach ($data as $score)
  {
      $round_names[] = $score-&gt;treatment;
      $table[$score-&gt;testRecipeID][$score-&gt;treatment] = $score-&gt;value;
      $total[$score-&gt;testRecipeID] += $score-&gt;value;
      print_r($round_names);
  
  }
  
  
  $round_names = array_unique($round_names);
  
  foreach ($table as $player =&gt; $rounds)
  {
      echo "$player\t";
      foreach ($round_names as $round)
          echo "$rounds[$round]\t";
      echo "$total[$player]\n";
  }
  </code></pre>
  
  <p>any help will be greatly appreciated!</p>
  
  <p>this is how i do it in JS</p>
  
  <pre><code>var result = [];
  data.forEach(function(e) {
    var a = e.model_num + '|' + e.revision+ '|'e.recipeID;
     if(!this[a]) {
      this[a] = {model_num: e.model_num, revision: e.revision, recipeID: e.recipeID}
      result.push(this[a]);
    }
    this[a][e.treatment] = e.value;
  }, {});
  </code></pre>